FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


HTTP Fileupload and CHMOD





ashok
Hi, I'm trying to create a script which allows users to upload images to my website (Of course, files are fully filtered, only jpg,gif,png files are allowed, and upload directory has no permissions for execute). My code is as follows:

HTML File:
Code:
<form enctype="multipart/form-data" action="upload_image.php" method="POST">
<input type="hidden" name="form_submitted" value="1" />
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>


Upload PHP File (I've removed the filtering code to make it simple for reading):
Code:
$target_path = "upd_img/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
    $filename = trim($_FILES['uploadedfile']['name']);
    echo "Successful!";
}
else
{
    echo "Failed!";
}


The problem is move_uploaded_file is setting the CHMOD to the uploaded file to 600, means No read access for World too! Image is getting uploaded, but cannot be viewed back through html files. A little googling lead me here

Since direct CHMOD isn't working, I worked around the issue using FTP CHMOD.
Code:

$target_path = "upd_img/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
    $filename = trim($_FILES['uploadedfile']['name']);
    $ftp_server = "myftpserveraddress";
    $ftp_user_name = "ftpuser";
    $ftp_user_pass = "ftppass";

    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    if ((!$conn_id) || (!$login_result))
   {
           echo "FTP connection has failed!";
           echo "Attempted to connect to $ftp_server for user $ftp_user_name";
           exit;
    }
    echo ftp_chmod($conn_id, 0604, "upd_img/".$filename) ? "CHMOD successful!" : 'Error';
    ftp_close($conn_id);
    echo("Successful!");
}
else
{
    echo("Failed!");
}


That's how I'm working around the limitation. But clearly the FTP logging, FTP CHMODDING is taking it's time (seconds do count in view of users, isn't it?). Is there any way of CHMOD() directly? If there isn't a way, can you change move_uploaded_file() CHMOD default to 604 instead of 600? Just like what's done here, can you too “patch” our versions of PHP to always make move_uploaded_file() give mode 0604?

Thanks for your time Smile
eznet
Hello Ashok,
Have you tried:
Code:
<?php
chmod("/somedir/somefile", 0604); 
?>


The original article can be found at : http://us3.php.net/chmod

Hope this helps,

-Matt
ashok
Yes, I tried, and from the same source:

Quote:
Note: The current user is the user under which PHP runs. It is probably not the same user you use for normal shell or FTP access. The mode can be changed only by user who owns the file on most systems.

Note: This function will not work on remote files as the file to be examined must be accessible via the servers filesystem.

Note: When safe mode is enabled, PHP checks whether the files or directories you are about to operate on have the same UID (owner) as the script that is being executed. In addition, you cannot set the SUID, SGID and sticky bits.


and hence, i think i cannot use chmod from the http file upload script... If there is a way we can use chmod in the above script without using ftp, and we can set user(owner of file) please throw in some code so that i can try...
Related topics
CHMOD Tutorial
Chatroom
chmod
chmod
FAQ - CHMOD nedir,nasýl kullanýlýr?
CHMOD?! Help!
Can´t CHMOD subdirectories
MyBB and CHMOD with myAdmin
CHMOD 0777
CHMOD Question
[Ajuda] CHMOD
Probléme CHMOD 777
chmod 000
Unable to CHMOD - related domainstat hack
Reply to topic    Frihost Forum Index -> Support and Web Hosting -> Frihost Support

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.