[英]PHP Image not uploaded to website
在我的網站上,我有2個單獨的位置供用戶上傳圖像。 第一個完美。 第二個沒有。 據我所知,它們是完全相同的。 為了進行測試,我在兩個圖像上使用了相同的圖像,並且在第一個圖像上起作用,但第二個圖像卻沒有。 這是不工作的代碼:
<?php
include('cn.php');
$name = $_FILES['image']['name'];
$type = $_FILES['image']['type'];
$size = $_FILES['image']['size'];
$temp = $_FILES['image']['tmp_name'];
$error = $_FILES['image']['error'];
echo $name; //Doesnt echo anything
$rand_num = rand(1, 1000000000);
$name = $rand_num . "_" . $name;
echo $name; //Echos just the random number followed by _
if ($error > 0) {
die("Error uploading file! Go back to the <a href='gallery.php'>gallery</a> page and try again.");
} else {
$sql = "INSERT INTO gallery (image) VALUES ('".$name."')"; //Inserts the random number to the database
$query = mysql_query($sql) or die(mysql_error());
echo $sql;
move_uploaded_file($temp, "gallery_pics/$name"); //Doesn't move the file. gallery_pics exists, if that matters
echo "Upload complete! Go back to the <a href='gallery.php'>album</a>.";
}
?>
如果有人可以在這里幫助我。 我確信這很簡單,但是我還沒有找到任何幫助的方法。 謝謝!
編輯:代碼上方有兩行無法正確顯示。 一個啟動PHP,另一個打開數據庫。
兩種腳本都位於同一目錄中嗎? 如果沒有,請確保指定正確的相對路徑或完整路徑,並在其前面加上如下斜杠:
move_uploaded_file($temp, "/some_folder/gallery_pics/$name");
另外,您是否檢查上傳文件的擴展名? 如果沒有,則用戶可以上傳並執行一個PHP文件。 順便說一下,您的文件名看起來會更好:
$rand_num = rand(1111111111, 9999999999);
開發或嘗試查找故障時啟用錯誤報告,還要檢查服務器錯誤日志。 檢查$_FILES['image']['error']
錯誤代碼,並相應顯示錯誤。 閱讀下面的代碼。 希望能幫助到你
<?php
//Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors',1);
$error_types = array(
0=>"There is no error, the file uploaded with success",
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder"
);
if($_FILES['image']['error']==0) {
// process
$name = $_FILES['image']['name'];
$type = $_FILES['image']['type'];
$size = $_FILES['image']['size'];
$temp = $_FILES['image']['tmp_name'];
//mt_rand() = 0-RAND_MAX and make filename safe.
$name = mt_rand()."_".preg_replace('/[^a-zA-Z0-9.-]/s', '_', $name);
//insert into db, swich to PDO
...
//Move file upload
if (move_uploaded_file($_FILES['image']['tmp_name'], "gallery_pics/$name")) {
echo "Upload complete! Go back to the <a href='gallery.php'>album</a>.";
} else {
echo "Failed to move uploaded file, check server logs!\n";
}
} else {
// error
$error_message = $error_types[$_FILES['userfile']['error']];
die("Error uploading file! ($error_message)<br/>Go back to the <a href='gallery.php'>gallery</a> page and try again.");
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.