469,928 Members | 1,862 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,928 developers. It's quick & easy.

I can't used chmod in my php upload. Please Help Me!!!!

I used this script and test on my hosting.
Evertimes appear "change permission to 777 failed. ".
So, how to solve this error.

Please kindly check for me and teach me.


$MAX_SIZE = 2000000;
$FILE_EXTS = array('.zip','.jpg','.png','.gif','.doc');
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$upload_dir = "files/";
$upload_url = $url_dir."/files/";
$message ="";
if (!is_dir("files")) {
if (!mkdir($upload_dir))
die ("upload_files directory doesn't exist and creation failed");
if (!chmod($upload_dir,0777))
die ("change permission to 777 failed.");
if ($_REQUEST[del] && $DELETABLE) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."DELETE - $_SERVER[REMOTE_ADDR]"."$_REQUEST[del]\n");

if (strpos($_REQUEST[del],"/.")>0); //possible hacking
else if (strpos($_REQUEST[del],$upload_dir) === false); //possible hacking
else if (substr($_REQUEST[del],0,6)==$upload_dir) {
print "<script>window.location.href='$url_this?message=d eleted successfully'</script>";
else if ($_FILES['userfile']) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]"
.$_FILES['userfile']['name']." "

$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = strtolower(substr($file_name,strrpos($file_name,". ")));

//File Size Check
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
$message = "The file size is over 2MB.";
//File Extension Check
else if (!in_array($file_ext, $FILE_EXTS))
$message = "Sorry, $file_name($file_type) is not allowed to be uploaded.";
$message = do_upload($upload_dir, $upload_url);

print "<script>window.location.href='$url_this?message=$ message'</script>";
else if (!$_FILES['userfile']);
$message = "Invalid File Specified.";

$filelist = "";
while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {
$filelist .= "<a href='$upload_dir$file'>".$file."</a>";
$filelist .= " <a href='?del=$upload_dir".urlencode($file)."' title='delete'>x</a>";
$filelist .= "<sub><small><small><font color=grey> ".date("d-m H:i", filemtime($upload_dir.$file))
$filelist .="<br>";

function do_upload($upload_dir, $upload_url) {

$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
$file_path = $upload_dir.$file_name;

if ( $file_name =="") {
$message = "Invalid File Name Specified";
return $message;

$result = move_uploaded_file($temp_name, $file_path);
if (!chmod($file_path,0777))
$message = "change permission to 777 failed.";
$message = ($result)?"$file_name uploaded successfully." :
"Somthing is wrong with uploading a file.";
return $message;


<font color=red><?=$_REQUEST[message]?></font>
<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
Upload File <input type="file" id="userfile" name="userfile">
<input type="submit" name="upload" value="Upload">

<br><b>My Files</b>
<hr width=70%>
<hr width=70%>
<small><sup>Developed By
<a style="text-decoration:none" href="http://tech.tailoredweb.com">TailoredWeb.com</a>

Apr 4 '07 #1
0 1584

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Murali Inguva | last post: by
1 post views Thread by Rahul | last post: by
1 post views Thread by hazel.montano | last post: by
5 posts views Thread by tabani | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.