PHP图像未上载到网站

PHP图像未上载到网站,php,Php,在我的网站上,我有两个独立的地方供用户上传图片。第一种方法非常有效。第二个没有。据我所知,他们完全一样。为了测试,我在两者上都使用了相同的图像,在第一个图像上工作,但在第二个图像上不工作。以下是非工作模式的代码: <?php include('cn.php'); $name = $_FILES['image']['name']; $type = $_FILES['image']['type']; $size = $_FILES['image']['size']; $temp = $_FI

在我的网站上,我有两个独立的地方供用户上传图片。第一种方法非常有效。第二个没有。据我所知,他们完全一样。为了测试,我在两者上都使用了相同的图像,在第一个图像上工作,但在第二个图像上不工作。以下是非工作模式的代码:

<?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>.";
}

?>

两个脚本是否位于同一目录中?如果没有,请确保指定正确的相对路径或完整路径,前面加斜杠,如下所示:

move_uploaded_file($temp, "/some_folder/gallery_pics/$name");
此外,您是否检查上载文件的扩展名?如果没有,用户可以上传并执行一个PHP文件,例如。顺便说一句,这样您的文件名看起来会更好:

$rand_num = rand(1111111111, 9999999999);

在开发或试图查找故障时启用错误报告,同时检查服务器错误日志。检查
$\u文件['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.");
}
?>


两个位置都在同一台服务器上?请把HTML代码放上去好吗?两个位置都将文件移动到gallery_图片中?检查该文件夹的权限。您是否正在发布到此页面本身?第一个会将其实际移动到用户图片。。这可能就是问题所在。。我去看看。