网站贝克图片上传问题

时间:2011-07-06 20:01:28

标签: php

最近我正在开发网站baker的模块。上传图片有一个选项。我使用的是php图片上传代码,但是出现以下错误,图片没有上传。

Warning: move_uploaded_file(http://localhost/wb/media/gallery/mypic.jpg) [function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections in C:\wamp\www\wb\modules\hotelmod\save_picture.php on line 84

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\phpBBBD.tmp' to 'http://localhost/wb/media/gallery/mypic.jpg' in C:\wamp\www\wb\modules\hotelmod\save_picture.php on line 84

这是我使用的代码:

if (($_FILES["image"]["type"] == "image/jpeg" || $_FILES["image"]["type"] == "image/pjpeg" || $_FILES["image"]["type"] == "image/gif" || $_FILES["image"]["type"] == "image/x-png") && ($_FILES["image"]["size"] < 4000000))
    {

        // if uploaded image was JPG/JPEG
        if($_FILES["image"]["type"] == "image/jpeg" || $_FILES["image"]["type"] == "image/pjpeg"){  
            $image_source = imagecreatefromjpeg($_FILES["image"]["tmp_name"]);
        }       
        // if uploaded image was GIF
        if($_FILES["image"]["type"] == "image/gif"){    
            $image_source = imagecreatefromgif($_FILES["image"]["tmp_name"]);
        }   
        // BMP doesn't seem to be supported so remove it form above image type test (reject bmps)   
        // if uploaded image was BMP
        if($_FILES["image"]["type"] == "image/bmp"){    
            $image_source = imagecreatefromwbmp($_FILES["image"]["tmp_name"]);
        }           
        // if uploaded image was PNG
        if($_FILES["image"]["type"] == "image/x-png"){
            $image_source = imagecreatefrompng($_FILES["image"]["tmp_name"]);
        }


        $remote_file =  WB_URL.'/modules/hotelmod/images/'.$_FILES["image"]["name"];
        move_uploaded_file($_FILES['image']['tmp_name'], $remote_file);
}

我该如何解决这个问题?提前致谢。

3 个答案:

答案 0 :(得分:1)

你可能会尝试做类似的事情:

move_uploaded_file($_FILES['somefile']['tmp_name'], 'http://localhost/wb/media/gallery/mypic.jpg');

这是不正确的。尝试写入URL只会触发另一个HTTP上传,这是您已经尝试处理的内容。

move_uploaded_files()的目标/目标必须是文件系统路径,例如

/home/sites/yoursite/document_root/images/mypic.jpg

代替。

答案 1 :(得分:1)

您的目标必须是文件系统路径,而不是URL。由您的Web服务器将URL映射到文件系统路径以查找文件(当然,您的脚本需要知道它是如何做到的,以便您可以创建指向它的链接)。

例如,您可以将服务器配置为从/srv/www/example.com/uploads/提供http://example.com/uploads/。然后你就可以让你的PHP脚本将文件移动到/srv/www/example/com/uploads/FOO.png。

出于安全原因,请务必关闭上传目录中的PHP(和CGI等)。例如,在Apache中,你会这样做:

<Directory /srv/www/example.com/uploads>
    php_admin_flag engine off
</Directory>

答案 2 :(得分:1)

您正在将网址传递给move_uploaded_file()move_uploaded_file()需要一条路。不是网址。可能类似于(对于Windows)C:\path\to\save\uploads