Monday, February 5, 2024

[SOLVED] chmod() a directory on localhost

Issue

I'm currently trying out an upload form for .csv files on localhost, however I haven't been able to upload a file with it yet. This is because I need to chmod a directory on localhost. Currently I'm using this:

$allowed_filetypes = array('.csv');
$max_filesize = 524288;
$upload_path = '/csvfiles/';

$filename = $_FILES['userfile']['name'];

// Check if we can upload to the specified path, if not DIE and inform the user.

if(!is_writable($upload_path)){
 $chmod = chmod ($upload_path & "/" & $_FILES, 777);

 // Upload the file to your specified path.
 if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)){
    echo 'Upload succesful';
    }else{
    echo 'There was an error during the file upload.  Please try again. :('; // It failed :(.
         }
    } else
    die ('You cannot upload to the specified directory, please CHMOD the directory.');

I'm not sure what this line of code needs to be:

$chmod = chmod ($upload_path & "/" & $_FILES, 777);

My script keeps dying, and nothing happens to the directory.

Also, if you have experience with these kind of things, feel free to debug! :D

Side note: My OS is RHEL5

Thanks in advance,

-Max


Solution

You're trying to concatenate the $_FILES array to a string to create the final path to be chmodded and that's probably the problem. If I understand this correctly, you need to change this to:

$chmod = chmod ($upload_path & "/" & $filename, 777);

or

$chmod = chmod ($upload_path & "/" & $_FILES['userfile']['name'], 777);

so you append the file name to $upload_path.



Answered By - Thorsten Dittmar
Answer Checked By - Marilyn (WPSolving Volunteer)