doubi1931 2012-03-06 23:19
浏览 48
已采纳

图像上载脚本错误

I'm using this php code for an image upload. But I keep getting an error towards the end. The result "Error while uploading image. Please Try Again. I feel like I have checked everything but the dang image just wont copy into the uimages folder. Please help...

Here is the code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php 

function create_thumbnail($source,$destination,$thumb_width){

    $size = getimagesize($source);
    $width = $size[0];
    $height = $size[1];
    $x = 0;
    $y = 0;

    if($width > $height){

        $x = ceil(($width - $height) / 2);
        $width = $height;
        }
            else if($height> $width){
            $y =(($height - $width) /2);
            $height = $width;
}

$new_image = imagecreatetruecolor($thumb_width,$thumb_height) or die('Cannot Initiatlize new GD image stream');

$extension = get_image_extension($source);
if($extension=='jpg' || $extension=='jpeg')
    $image= imagecreatefromjpeg($source);
if($extension=='gif')
    $image= imagecreatefromgif($source);
if($extension=='png')
    $image= imagecreatefrompng($source);


imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
if($extension=='jpg' || $extension=='jpeg')
    imagejpeg($new_image,$destination);
if($extension=='gif')
    imagegif($new_image,$destination);
if($extension=='png')
    imagepng($new_image,$destination);

}

function get_image_extension($name){
    $name=strtolower($name);
    $i = strrpos($name,".");
    if(!$i) {return "";}
        $l = strlen($name) - $i;
        $extension = substr($name,$i+1,$l);
        return $extension;  
    }

function random_name($length){
    $characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $name = "";

    for($i = 0; $i < $length; $i++){
        $name .= $characters[mt_rand(0,strlen($characters) - 1)];

        }
        return "image-".$name;

    }   

    $images_location = "images";
    $thumbs_location ="uimages/thumbs";
    $thumb_width = 100;
    $maximum_size = 5120;

    $results = "Click to browse to locate the image you want to upload.";

    if($_POST){
        if($_FILES['image']['name'] == "")
{
    $results="No Image Selected. Click Browse to find an image";

    }   
    else{
        $size=filesize($_FILES['image']['tmp_name']);
        $filename = stripslashes($_FILES['image']['name']);
        $extension = get_image_extension($filename);
        if($size> $maximum_size){
            $results="Your file exceeds the maximum size.";

            }
        else
        if(($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")){
            $results="Invalid image type. Must be jpg, jpeg, png, or a gif file.";
            }
            else{
                $image_random_name = random_name(15).".".$extension;
    $copy= @copy($_FILES['image']['tmp_name'], $images_location.$image_random_name);

if(!$copy){
    $results = "Error while uploading image. Please try again.";}
    else{
        create_thumbnail($images_location.$image_random_name,$thumbs_location.$image_random_name,$thumb_width);
        $results="Image has been uploaded";

        }

    }


                }
        }   



?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="5120" >
<input type="file" name="image" />
<input type="submit" value="Upload Image" />
</form>

<?php echo $results; ?>

</body>
</html>
  • 写回答

2条回答 默认 最新

  • doushouhe7072 2012-03-06 23:23
    关注

    Try moving the file like this:

    if (move_uploaded_file($_FILES['image']['tmp_name'], $images_location.$image_random_name))
    {
        //success
    }
    else
    {
        //fail
    }
    

    Also check that the target location exists and is being set correctly, and that permissions of the target location are correct- Ensure that apache (or whatever is running) has the correct write access. To set the permissions for apache it is something like this:

    chown -R nobody directory_name
    chmod 755 -R directory_name
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建