2014-04-25 29 views
-5

這些天我正在設計一個文件上傳系統。 我想上傳圖片到服務器並將文件路徑存儲在MySQL數據庫中。 保存文件路徑後,用戶會在網址被重定向回上傳頁面與用戶字符串uid=1上傳文件,插入文件路徑到MySQL並返回到URL中的字符串上傳頁面

upload.php的

<form action="<?php echo "upload_file.php?id=" . $cid ; ?>" method="post" enctype="multipart/form-data" name="photo" id="photo"> 
     <table width="295" border="0" align="center"> 
      <tr> 
      <td width="289">Photos</td> 
      </tr> 
      <tr> 
      <td class="formdisplay"><label for="priestname"></label> 
       <label for="photo"></label> 
      <input type="file" name="file" id="file" /></td> 

      </tr> 
      <tr> 
      <td class="formdisplay"><input type="submit" name="submit" id="submit" value="Submit" /></td> 
      </tr> 
     </table> 
     </form> 

$cid是利用URL檢索用戶ID的變量GET [upload.php?id=1]

並幫我創建upload_file.php文件。

+2

你想我們爲你寫整個文件嗎? http://www.w3schools.com/php/php_file_upload.asp – Justinas

+0

如果任何答案的工作公平並接受它......如果不是,問。 –

回答

2

首先form需要有enctype="multipart/form-data" 現在在你的情況下,我會創建另一個hidden輸入,而不是在動作中使用變量。

下面是一個示例我使用

//use this to get the extension of the file 
function findexts ($filename) 
{ 
    $filename = strtolower($filename) ; 
    $exts = split("[/\\.]", $filename) ; 
    $n = count($exts)-1; 
    $exts = $exts[$n]; 
    return $exts; 
} 
    $ext = findexts ($_FILES['photo']['name']) ; 
    //create files name 
    $player_id2 = $player_id."."; 
    $imgforDB = $player_id2.$ext; 
    //the directory which you want to store the file 
    $target = "img/players/"; 
    $target = $target . $player_id2.$ext; 
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
     {   
      //Tells you if its all ok 
      echo "The file ". basename($_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory"; 
      $sql2 = $mysqli->query("UPDATE **** SET `player_img`='$imgforDB' WHERE ***'") 
      or die(mysqli_error($mysqli)); 
      } 
      else { 
      //Gives and error if its not 
      echo "Sorry, there was a problem uploading your file."; 
    } 

這是相當多的整體思路。在DB這將是商店,就像這樣player12.jpg

+0

不工作 推薦使用:功能拆分()在/home4/diocol/public_html/churchinfo/upload_file.php已經過時上線6 – Stacky

1

我希望我的樣本將幫助:

<? 
ob_start(); 
$uploaddir = 'path_to_file/'; 
$uploadfile = $uploaddir . basename($_FILES['file']['name']); 
echo $uploadfile; 
if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) { 
    echo "Upload error.\n"; 
} 

//Query to store path for eample with PDO 
$q = $conn_xyz->prepare("insert into images (path) Values ('".$uploadfile."')"); 
$q->execute(); 
//Store $cid in hidden form element then it is post, else it will be GET 
header("Location: upload_form.php?cid=".$_GET["cid"]); 
?> 

你對我們的代碼中的一些錯誤:

<form action="<?php echo "upload_file.php?id=" . $cid ; ?>" method="post" enctype="multipart/form-data" name="photo" id="photo"> 
      <table width="295" border="0" align="center"> 
       <tr> 
       <td width="289">Photos</td> 
       </tr> 
       <tr> 
       <td class="formdisplay"><label for="priestname"></label> 
        <label for="photo"></label> 
       <input type="file" name="file" id="file" /></td> 

       </tr> 
       <tr> 
       <td class="formdisplay"><input type="submit" name="submit" id="submit" value="Submit" /></td> 
       </tr> 
      </table> 
      </form> 

好代碼是:

<form action="<?php echo "process.php?cid=".$cid.""; ?>" method="post" enctype="multipart/form-data" name="photo" id="photo"> 
     <table width="295" border="0" align="center"> 
      <tr> 
      <td width="289">Photos</td> 
      </tr> 
      <tr> 
      <td class="formdisplay"><label for="priestname"></label> 
       <label for="photo"></label> 
      <input type="file" name="file" id="file" /></td> 

      </tr> 
      <tr> 
      <td class="formdisplay"><input type="submit" name="submit" id="submit" value="Submit" /></td> 
      </tr> 
     </table> 
     </form> 

看到你寫的id = not cid =的表格的第一行,並且缺少一些引號。

+0

我看不出有任何缺失報價。你使用JavaScript重定向回表單?什麼? –

+0

是的,我審查速度快,你是對的。是的,我用Javascript重定向,我不得不說我不是一個PHP專業人員,我是一個系統管理員:-) –

+0

你寧願設置一個'header()'。 –