2016-09-23 56 views
-2

您好,這是我的代碼上傳圖像數據庫和文件夾,它不工作上傳圖像數據庫和文件夾

這裏的代碼

if ($_REQUEST[completed] == 1 || $_REQUEST[register] == 'register') 
{ 

    $target_path = "../imagess/"; 

    $rand1=rand(1,1000000); 
    $target_path1 = $target_path .$rand1; 
    if(basename($_FILES['uploadedfile1']['name'])=="") 
    { 
     move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1); 
     $alltarget1="imagess/".basename($_FILES['uploadedfile1']['name']);} 
    else 
    { 
     $var1=$rand1; 
     move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1); 
     $alltarget1="imagess/".$var1;}<form method="post"> 

    <tr> 
     <td> 
      <input type="hidden" name="MAX_FILE_SIZE" value="10000000000" /> 
      <input type=hidden name=completed value=1> 
      Choose a Picture to upload: [1991 x 1241] <input name="uploadedfile1" type="file" />w: 1920px | h: 503px<br /><br /> 
      <br/> 
     </td> 
    </tr> 

    <tr> 
     <input type="hidden" name="register" value="register"/> 
     <td><input type="submit" value="Insert "/> 
      <input type="reset" value="Reset"/> 
     </td> 
    </tr> 
</form> 

任何一個可以幫助我,請

+0

'{$ VAR1 = $ RAND1; move_uploaded_file($ _ FILES ['uploadedfile1'] ['tmp_name'],$ target_path1); $ alltarget1 =「imagess /".$ var1;}

'單獨應該拋出一個解析錯誤。另外,你錯過了一個有效的enctype。 –

+0

我的代碼應該是什麼?將圖像保存到數據庫中,但不顯示在文件夾中!這是我的問題,謝謝:) – eddy10394

回答

1

製作確保您的文件夾存在

../imagess/

$target_path = "../imagess/"; 
move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1); 

,包括enctype="multipart/form-data"到您的表單,如

<form enctype="multipart/form-data" > 
<input name="uploadedfile1" type="file"> 
... 
</form> 
0

檢查你的HTML代碼code.Form應該是這樣的。

<form method="post" action="" enctype="multipart/form-data"> 
<tr> 
    <td> 
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000000" /> 
    <input type=hidden name=completed value=1> 
    Choose a Picture to upload: [1991 x 1241] 
    <input name="uploadedfile1" type="file" />w: 1920px | h: 503px<br /><br /> 

    <br/> 
    </td> 
    </tr> 
    <tr> 
     <input type="hidden" name="register" value="register"/> 
     <td> 
     <input type="submit" value="Insert "/> 
     <input type="reset" value="Reset"/> 
    </td> 
    </tr> 
</form> 

並檢查上傳文件夾路徑是否正確。

$target_path = "../imagess/"; 
+0

該文件夾是好的,圖像保存在文件夾現在但問題是,保存爲一個隨機數的文件,而不是真實的圖像 – eddy10394

0

對於包括通過形式的多媒體 ENCTYPE應該

檢查此鏈接 https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

HTML表單

<?php 
require('main.php'); 
if ($_POST) { 
    $r = base::functionName($_POST, $_FILES); 
    if ($r == 'true') { 
     header("Location: your-page.php"); 
    } 
} 
?> 
<form id="thisId" method="post" enctype="multipart/form-data"> 
    <label>Cordinator Image <span class="required"></span></label> 
      <div class="da-form-item large"> 
       <input type="file" name="imagepath" class="da-custom-file"/> 
       <label for="imagepath" class="error" generated="true" style="display:none;"></label> 
      </div> 
    <div> 
     <input type="submit" value="Submit"> 
    </div> 
</form> 

PHP代碼

main.php 
<?php 

class sql{ 
     static function insert($table, $values){ 
     // Insert PDO function 
    } 
} 
class base extends sql 
{ 

    static function functionName($post,$files){ 
       $filename=$files['imagepath']['name']; 
       if(!empty($filename)){ 
        $tmp_name=$files['imagepath']['tmp_name']; 
        $rand=rand(1000,9999); 
        $dst='../imagefolder/'.$rand.$filename; 
        move_uploaded_file($tmp_name, $dst); 
        $post['imagepath']=$rand.$filename; 
       } 
       $r=parent::insert('tablename', $post); 
       $r=is_numeric($r)?'true':'false'; 
       return $r; 
      } 
} 

檢查此鏈接 https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

相關問題