views:

303

answers:

1

Can you please tell me if there are ways to upload images with php without chmoding the directory with

`ftp_site($conn_id, 'CHMOD 0777, /www/images/thumbs');`

to circumvent [function.move-uploaded-file]: failed to open stream: Permission...

Thanks!

+2  A: 

absolutely.

I'm not sure why you're using ftp_site(). Are you FTPing the uploaded file to another server, or just trying to make an upload form that uploads to the same machine that's serving the php file?

Assuming you're working on uploads to the server that is running the script, there are a few things. Make sure the user your web server is running as (httpd, apache, lighttpd, or similar) has write access to the $uploadPath. To do this, you can chmod 0777 at it, but this is insecure, as any user on the system can now write to this folder, and we only want apache to be able to. Contact me via http://yaauie.com/me if you need help setting this part up; I'm not sure how comfortable you are at the command line and don't want to overwhelm you with jibberjabber

Here's some quick procedural code that might help you troubleshoot where you're getting caught; point your upload form at this script to test.

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