2015-12-07 224 views
1

我正在開發一個帶有文件上傳的Web應用程序。PHP文件上傳move_uploaded_file不起作用

我剛寫了一段php代碼來上傳圖片。

if(isset($_FILES['equipmentPictureName'])){ 

$file_tmp =$_FILES['equipmentPictureName']['tmp_name']; 
$file_name = $_FILES['equipmentPictureName']['name']; 

$uploads_dir = '/pic'; 
echo "on file upload if"; 
if(move_uploaded_file($file_tmp,$uploads_dir.$file_name)){ 
    echo "uploaded"; 
    exit(); 
}else 
{ 
    echo "error on upload"; 
    exit(); 
} 
}else{ 
echo "File Not Present"; 
exit(); 
} 

當我運行這段代碼,我可以在文件上傳得到像輸出在上傳錯誤

我給777權限PIC文件夾

問題是什麼,任何想法,,

表..

<form action="manage-ahri-action.php" method="post" name="equipment" enctype="multipart/form-data" id="multiple_upload_form" > 

    <div class="col-lg-6 col-sm-6 row" style="margin-bottom: 15px;"> 
          <div class="form-group"> 
           <label for="" class="col-lg-4 col-sm-4" style="text-align: right;">Equipment Picture</label> 
           <div class="col-lg-8 col-sm-8"> 
            <div class="option-group uploading none"> 
             <span class="file-button btn-primary" style="margin-right: 14px;">Choose File</span> 
             <input name="equipmentPictureName" type="file" class="form-control form-white"/> 
            </div> 
           </div> 
          </div> 
         </div> 

         <div class="file col-lg-12 col-sm-12 col-md-12"> 
          <button type="submit" class="btn btn-primary btn-square" style="margin-top: 20px">Submit</button> 
         </div> 

</form> 

的print_r

陣列([equipmentPictureName] =>陣列([名稱] =>貓01.JPG [型] =>圖像/ JPEG [tmp_name的值] =>的/ tmp/php96PS5A [錯誤] => 0 [數組] => [數組] =>數組([數組] => 72413)[認可] =>數組([名稱] => [類型] => [tmp_name] => [錯誤] =>名] => [式] => [tmp_name的值] => [錯誤] => 4 [尺寸] => 0))

由於

+1

'/'在'$ uploads_dir。$ file_name'之間它會是'$ uploads_dir。「/」。$ file_name' – Saty

+0

不工作...我試過 –

+0

請將您的表單代碼!!! – Saty

回答

0

嘗試此修復到您的代碼:

$uploads_dir = '/pic/'; //Add another slash to complete the directory path 
+0

不工作...我試過 –

0

//獲取上傳目錄的路徑。

$wp_upload_dir = wp_upload_dir(); 
$uploads_dir= $wp_upload_dir['url'] . '/pic/' ; 
+0

不工作.... –

0

試試這個:

if(isset($_FILES['equipmentPictureName']['name'])){ 

$file_tmp =$_FILES['equipmentPictureName']['tmp_name']; 
$file_name = $_FILES['equipmentPictureName']['name']; 

$uploads_dir = 'pic/'; 
echo "on file upload if"; 
$move=move_uploaded_file($file_tmp,$uploads_dir.$file_name); 
if($move){ 
    echo "uploaded"; 
    exit(); 
}else 
{ 
    echo "error on upload"; 
    exit(); 
} 
}else{ 
echo "File Not Present"; 
exit(); 
} 

,並確保該文件和文件夾都在同一個位置。該文件夾有777權限。

編輯:

或代替這主要條件

if(isset($_FILES['equipmentPictureName']['name'])){ 

您可以檢查使用,這就意味着替換此行:你需要

if($_FILES['file_equipmentPictureName']['name']!=''){