简体   繁体   English

PHP映像未上传到网站

[英]PHP Image not uploaded to website

On my website I have 2 separate places for users to upload an image. 在我的网站上,我有2个单独的位置供用户上传图像。 The first works perfectly. 第一个完美。 The second doesn't. 第二个没有。 As far as I can see, they are exactly the same. 据我所知,它们是完全相同的。 To test, I used the same image on both, with it working on the first, but not the second. 为了进行测试,我在两个图像上使用了相同的图像,并且在第一个图像上起作用,但第二个图像却没有。 Here is the code from the non-working one: 这是不工作的代码:

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

?>

If someone could please help me out here. 如果有人可以在这里帮助我。 I am sure it is something simple, but I have yet to find anything that helped. 我确信这很简单,但是我还没有找到任何帮助的方法。 Thanks! 谢谢!

EDIT: There are two lines above the code that won't show up properly. 编辑:代码上方有两行无法正确显示。 One starts the php and the other opens the database. 一个启动PHP,另一个打开数据库。

Do both scripts reside in the same directory? 两种脚本都位于同一目录中吗? If not, make sure you specify proper relative or a full path, preceded by slash like this: 如果没有,请确保指定正确的相对路径或完整路径,并在其前面加上如下斜杠:

move_uploaded_file($temp, "/some_folder/gallery_pics/$name");

Also, do you check the extensions of uploaded files? 另外,您是否检查上传文件的扩展名? If not, the user could upload and execute a PHP file, for example. 如果没有,则用户可以上传并执行一个PHP文件。 And by the way, your file name would look better this way: 顺便说一下,您的文件名看起来会更好:

$rand_num = rand(1111111111, 9999999999);

Enable error reporting when developing or trying to find a fault also check your server error logs. 开发或尝试查找故障时启用错误报告,还要检查服务器错误日志。 Check for the $_FILES['image']['error'] error code and display the error accordingly. 检查$_FILES['image']['error']错误代码,并相应显示错误。 Have a go with the code below. 阅读下面的代码。 hope it helps 希望能帮助到你

<?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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM