![](/img/trans.png)
[英]move uploaded image into folder in php(move_uploaded_file() function)
[英]move uploaded file into folder in php
我使用下面的代碼將圖像更新到表中,它正在更新圖像,但是在檢索圖像時路徑正確顯示,但圖像未顯示在頁面中。
if(isset($_FILES['file'])){
$errors= array();
$file_name = $_FILES['file']['name'];
$file_size =$_FILES['file']['size'];
$file_tmp =$_FILES['file']['tmp_name'];
$file_type=$_FILES['file']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));
$expensions= array("jpg","png","gif");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"C:\wamp\www\Mr.bazaar\images\ ".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
$a=$_POST['pids'];
echo $a;
$b=$_FILES['file']['name'];
echo $b;
$qry1=mysql_query("update brand set image='$b' where brand='$a'");
if($qry1)
echo "<script>alert('Your image Sucessgully Uploaded.');</script>";
else
echo "<script>alert('try again');</script>";
我使用下面的代碼在頁面中顯示圖像,但未在頁面中顯示
$result = mysql_query("select b_id,brand,image from brand");
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo '<tr>';
echo '<td>';
echo $row['brand'];
echo '</td><td>';
echo '<img height="30" width="60" src=../../images/';
echo $row['image'];
echo '>';
echo '</td>';
}
您上一個代碼中沒有引用image。 假設循環應如下所示:
while ($row = mysql_fetch_array($result)) {
$product = $row['brand'];
$image = $row['image'];
echo "<option value=\"$product\">$image</option>";
}
我認為有兩個錯誤。
首先在上載目錄中檢查文件是否實際被上載。 如果文件未上傳,則在move_uploaded_file函數中提供上傳目錄的相對路徑,即路徑應類似於“ ../../upload_directory”,而不應類似於“ c:/ somefolder /”或“ http://example.com” / abs “。
第二個錯誤是在您的圖像源聲明中
src=../../images/'; echo $row['image']; echo '>';
在這里,您缺少src后的雙引號,即(“”)。 應該像
src="../../images/'; echo $row['image']; echo '">';
請注意,第一行的=后面和尖括號>之前的雙引號(“)。
希望在更正上述兩個錯誤之后,您的代碼可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.