php图片上传和权限

时间:2009-03-11 03:36:35

标签: php file-upload

你能告诉我是否有办法用PHP上传图像而不用

修改目录
ftp_site($conn_id, 'CHMOD 0777, /www/images/thumbs');

绕过[function.move-uploaded-file]: failed to open stream: Permission...错误?

谢谢!

1 个答案:

答案 0 :(得分:4)

绝对

我不确定你为什么要使用ftp_site()。您是否将上传的文件FTP到另一台服务器,或者只是尝试上传到上传到提供php文件的同一台机器的上传表单?

假设您正在处理上传到运行脚本的服务器,那么有一些事情。确保您的Web服务器正在运行的用户(httpd,apache,lighttpd或类似的)具有对$ uploadPath的写访问权。要做到这一点,你可以chmod 0777,但这是不安全的,因为系统上的任何用户现在都可以写入这个文件夹,我们只希望apache能够。如果您需要帮助设置此部分,请通过http://yaauie.com/me与我联系;我不确定你在命令行上有多舒服,也不想用jibberjabber来压倒你

这里有一些快速的程序代码,可以帮助您解决被捕的问题;将您的上传表单指向此脚本进行测试。

<?php
// Set the upload path
$uploadPath = realpath('./images/thumbs/');

// test to see if the upload path is a directory that is writable
if(is_dir($uploadPath) && is_writable($uploadPath)) {

    // create the full path for the end result file
    $uploadFile = $uploadPath.basename($_FILES['userfile']['name']);

    // try to move the uploaded file
    if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile)) {
        echo 'Sucessfully moved file to "'.$uploadFile.'"';
    } else {
        echo 'Directory is writable, but we could not move the uploaded file to it.';
    }
} else {
    echo 'Either "'.$uploadPath.'" is not a directory, or it is not writable.';
}
?>