2014-04-29 6 views
0

사용자가 원격 데이터베이스에 파일을 업로드 할 수있는 페이지를 작성 중입니다.PHP 스크립트를 실행하고 결과를 동일한 페이지에 표시

사용자가 파일을 성공적으로 업로드하면 내 레이블에 "File Uploaded Successfully"가 표시됩니다.

파일 형식이 내 서버에서 허용하는 형식이 아닌 경우 레이블에 "잘못된 파일 형식이 표시됩니다. 다시 시도하십시오."

가능합니까?

<form method="post" role="form" action="/LS/import_export.html"> 

<div class="form-group"> 

<input type="file" name="file" id="file" size="75"> 
<label for="file">PHP Code here?</label> 

</div> 

<button type="submit" name="Import" value="Import">Upload</button> 

</form> 
+0

는 형태로 작업을 포기하고 대신에 그것의 클릭에 사용 버튼 JS 기능을 제출하지 마십시오 –

+0

난 당신이 사용하는 것이 좋습니다 <입력 버튼 대신에 type = "submit" –

답변

0

기본 사항을 시작하기위한 것입니다.

<?php 
    if(isset($_FILES["file"]) and $_FILES["file"]["tmp_name"] != ""){ 
     /* Do your validation here 
      Reference URL 
      http://www.w3schools.com/php/php_file_upload.asp 
     */ 

     if($move_file){ 
      $status = "File Uploaded Successfully"; 
     } else { 
      $status = "Incorrect file format. Please try again."; 
     } 
    } 
    ?> 

<form method="post" role="form" method="post" enctype="multipart/form-data"> 
    <div class="form-group"> 
    <input type="file" name="file" id="file" size="75"> 
    <label for="file">PHP Code here?</label> 
    </div> 
    <button type="submit" name="Import" value="Import">Upload</button> 
</form> 
0

예 당신은 당신이 사용 제한 할 수 있습니다

$aExceptedFormats = array('image/jpg', 'image/png'); 
if (!in_array($_FILES['file']['type'], $aExceptedFormats)) { 
    echo "Incorrect file format. Please try again."; 
} 
0

처럼 당신이 exceptable 파일 형식의 배열을 비교할 수 있습니다 업로드, 뭔가의

$_FILES['file']['type'] 

을 확인하는 것이 좋습니다 HTML5 수락 속성 사용 :

<input type="file" name="my-image" id="image" accept="image/gif, image/jpeg, image/png" /> 

또는 PHP를

$approved_types = array("image/png","image/jpg","image/jpeg"); 
$approved_exts = array("png","jpg","jpeg"); 
if (!in_array($_FILES['new_image']['type'], $approved_types)) { 
    die("Wrong type of file submitted press use your back button and try again."); 
    } 

Refernece 사용 : http://www.dreamincode.net/forums/topic/148485-restricting-file-types-and-size-in-upload-form/

0

이 빈에 action 속성을 변경하거나 $_SERVER['PHP_SELF']을 사용합니다.

양식 앞에 귀하의 POST 조치를 배치하십시오.

주 : 파일 업로드

예를 들어에 대한 사용 enctype="multipart/form-data".

if(isset($_POST['Import'])){ 
if ($_FILES["file"]["error"] > 0) { 
    echo "Error: " . $_FILES["file"]["error"] . "<br>"; 
} else { 
    echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
    echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
    echo "Size: " . ($_FILES["file"]["size"]/1024) . " kB<br>"; 
    echo "Stored in: " . $_FILES["file"]["tmp_name"]; 
} 
} 

<form method="post" role="form" action="/LS/import_export.html" enctype="multipart/form-data"> 

<div class="form-group"> 

<input type="file" name="file" id="file" size="75"> 
<label for="file">PHP Code here?</label> 

</div> 

<input type="submit" name="Import" value="Upload" /> 

</form> 

PHP 업로드에 대한 참고 자료. 당신이 here.

0
$con = mysql_connect(...) or die(mysql_error()); 
$db = mysql_select_db('database', $con); 
      if($db) 
       { 
        $query = "INSERT INTO upload(u_name,name,size,type,content) VALUES (specific values ...)"; 
        mysql_query($query) or die('Error, query failed'); 
        mysql_close(); 
        echo "<ul class='testimonials'> 
          <li> 
          <blockquote> 
           <center><span style='color: green;'>File Uploaded Successfully</span></center> 
          </blockquote> 
          </li> 
          </ul>"; 
       } 
       else 
       { 
        echo "<ul class='testimonials'> 
          <li> 
          <blockquote> 
           <center>span style='color: red;'>File Uploading Failed</span></center> 
          </blockquote> 
          </li> 
          </ul>"; 
       } 
      } 

이 시도 할 수 있습니다 확인할 수 있습니다 ..

관련 문제