2014-12-08 29 views
2

下面是給定的代碼。我想在上傳之前重命名文件。提交時顯示錯誤:未定義的索引。使用PHP重命名和上傳文件時出錯 - 未定義索引

 if($_FILES['FPScreenShot']['name']==true) 
     { 
      $SPPic = ($_FILES['FPScreenShot']['name']); 
      $curTime = time(); 
      $NewPriorPic = "prior"; 
      $NewPriorPic = $NewPriorPic.$SGeiNo; 
      $NewPriorPic = $NewPriorPic.$SSurgDt; 
      $NewPriorPic = $NewPriorPic.$curTime; 

      move_uploaded_file($_FILES['FPScreenShot']['tmp_name'] , "upload_pictures/".$_FILES['$NewPriorPic']['name']); 
     } 
    else 
     { 
      $SPPic = "NIL"; 
     } 
+0

您的代碼包含語法錯誤。 – Raptor 2014-12-08 08:19:03

+0

可能重複的[參考 - 這個錯誤在PHP中意味着什麼?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – 2014-12-08 08:19:35

+0

檢查您的使用在'move_uploaded_file'(' – RichardBernards 2014-12-08 08:19:35

回答

0

已解決。供將來參考,如果有人需要。

if($_FILES['FPScreenShot']['name']==true) 
     { 
      $SPPic = ($_FILES['FPScreenShot']['name']); 
      $ext = pathinfo($SPPic, PATHINFO_EXTENSION); 
      $curTime = time(); 
      $NewPriorPic = "prior"; 
      $NewPriorPic = $NewPriorPic.$SGeiNo; 
      $NewPriorPic = $NewPriorPic.$SSurgDt; 
      $NewPriorPic = $NewPriorPic.$curTime; 
      $NewPriorPic = $NewPriorPic.".".$ext; 
      $location = "upload_pictures/"; 

      move_uploaded_file($_FILES['FPScreenShot']['tmp_name'], $location.$NewPriorPic); 
     } 
1

我想你搞砸這條線一點點起來:

(你忘了開始報價)

move_uploaded_file($_FILES['FPScreenShot']['tmp_name'] , upload_pictures/".$_FILES['$NewPriorPic']['name']); 

所以改成這樣:

move_uploaded_file($_FILES['FPScreenShot']['tmp_name'] , "upload_pictures/" . $_FILES[$NewPriorPic]['name']); 
+0

引用是在那裏,可能是在複製粘貼我搞砸了。讓我編輯它有問題。 – Sandhu 2014-12-08 08:22:23

+0

@Sandhu引用哪一行錯誤你準確?並在哪一行? – Rizier123 2014-12-08 08:23:12

+0

錯誤是在相同的move_uploaded_files ....行。 – Sandhu 2014-12-08 08:25:23

0

有是你的代碼中的一些語法錯誤,你的邏輯似乎對你試圖實現的東西沒什麼困惑,但是你可以嘗試一下s:

<?php  
    if($_FILES['FPScreenShot']['name']) { 
      $SPPic = ($_FILES['FPScreenShot']['name']); 
      $curTime = time(); 
      $NewPriorPic = "prior"; 
      //Add aditional details to the file name 
      $NewPriorPic .= $SGeiNo; 
      $NewPriorPic .= $SSurgDt; 
      $NewPriorPic .= $curTime; 
      //Try to move uploaded file 
      if (move_uploaded_file($_FILES['FPScreenShot']['tmp_name'] , "upload_pictures/".$_FILES[$NewPriorPic]['name'])) { 
       echo "File successfully uploaded."; 
      } 
      else { 
       echo "Error while uploading the file."; 
      } 
     } 
    else { 
      $SPPic = "NIL"; 
     }