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.
[PHP]
<?php
$MAX_SIZE = 2000000;
$FILE_EXTS = array('.zip','. jpg','.png','.g if','.doc');
$DELETABLE = no;
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERV ER['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_fil es 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($resourc e,date("Ymd h:i:s")."DELETE - $_SERVER[REMOTE_ADDR]"."$_REQUES T[del]\n");
fclose($resourc e);
if (strpos($_REQUE ST[del],"/.")>0); //possible hacking
else if (strpos($_REQUE ST[del],$upload_dir) === false); //possible hacking
else if (substr($_REQUE ST[del],0,6)==$upload_ dir) {
unlink($_REQUES T[del]);
print "<script>window .location.href= '$url_this?mess age=deleted successfully'</script>";
}
}
else if ($_FILES['userfile']) {
$resource = fopen("log.txt" ,"a");
fwrite($resourc e,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]"
.$_FILES['userfile']['name']." "
.$_FILES['userfile']['type']."\n");
fclose($resourc e);
$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = strtolower(subs tr($file_name,s trrpos($file_na me,".")));
//File Size Check
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
$message = "The file size is over 2MB.";
//File Extension Check
else if (!in_array($fil e_ext, $FILE_EXTS))
$message = "Sorry, $file_name($fil e_type) is not allowed to be uploaded.";
else
$message = do_upload($uplo ad_dir, $upload_url);
print "<script>window .location.href= '$url_this?mess age=$message'</script>";
}
else if (!$_FILES['userfile']);
else
$message = "Invalid File Specified.";
$handle=opendir ($upload_dir);
$filelist = "";
while ($file = readdir($handle )) {
if(!is_dir($fil e) && !is_link($file) ) {
$filelist .= "<a href='$upload_d ir$file'>".$fil e."</a>";
if ($DELETABLE)
$filelist .= " <a href='?del=$upl oad_dir".urlenc ode($file)."' title='delete'> x</a>";
$filelist .= "<sub><small><s mall><font color=grey> ".date("d-m H:i", filemtime($uplo ad_dir.$file))
."</font></small></small></sub>";
$filelist .="<br>";
}
}
function do_upload($uplo ad_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.$fi le_name;
if ( $file_name =="") {
$message = "Invalid File Name Specified";
return $message;
}
$result = move_uploaded_f ile($temp_name, $file_path);
if (!chmod($file_p ath,0777))
$message = "change permission to 777 failed.";
else
$message = ($result)?"$fil e_name uploaded successfully." :
"Somthing is wrong with uploading a file.";
return $message;
}
?>
<center>
<font color=red><?=$_ REQUEST[message]?></font>
<br>
<form name="upload" id="upload" ENCTYPE="multip art/form-data" method="post">
Upload File <input type="file" id="userfile" name="userfile" >
<input type="submit" name="upload" value="Upload">
</form>
<br><b>My Files</b>
<hr width=70%>
<?=$filelist? >
<hr width=70%>
<small><sup>Dev eloped By
<a style="text-decoration:none " href="http://tech.tailoredwe b.com">Tailored Web.com</a>
</sup></small>
</center>
[/PHP]
Evertimes appear "change permission to 777 failed. ".
So, how to solve this error.
Please kindly check for me and teach me.
[PHP]
<?php
$MAX_SIZE = 2000000;
$FILE_EXTS = array('.zip','. jpg','.png','.g if','.doc');
$DELETABLE = no;
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERV ER['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_fil es 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($resourc e,date("Ymd h:i:s")."DELETE - $_SERVER[REMOTE_ADDR]"."$_REQUES T[del]\n");
fclose($resourc e);
if (strpos($_REQUE ST[del],"/.")>0); //possible hacking
else if (strpos($_REQUE ST[del],$upload_dir) === false); //possible hacking
else if (substr($_REQUE ST[del],0,6)==$upload_ dir) {
unlink($_REQUES T[del]);
print "<script>window .location.href= '$url_this?mess age=deleted successfully'</script>";
}
}
else if ($_FILES['userfile']) {
$resource = fopen("log.txt" ,"a");
fwrite($resourc e,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]"
.$_FILES['userfile']['name']." "
.$_FILES['userfile']['type']."\n");
fclose($resourc e);
$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = strtolower(subs tr($file_name,s trrpos($file_na me,".")));
//File Size Check
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
$message = "The file size is over 2MB.";
//File Extension Check
else if (!in_array($fil e_ext, $FILE_EXTS))
$message = "Sorry, $file_name($fil e_type) is not allowed to be uploaded.";
else
$message = do_upload($uplo ad_dir, $upload_url);
print "<script>window .location.href= '$url_this?mess age=$message'</script>";
}
else if (!$_FILES['userfile']);
else
$message = "Invalid File Specified.";
$handle=opendir ($upload_dir);
$filelist = "";
while ($file = readdir($handle )) {
if(!is_dir($fil e) && !is_link($file) ) {
$filelist .= "<a href='$upload_d ir$file'>".$fil e."</a>";
if ($DELETABLE)
$filelist .= " <a href='?del=$upl oad_dir".urlenc ode($file)."' title='delete'> x</a>";
$filelist .= "<sub><small><s mall><font color=grey> ".date("d-m H:i", filemtime($uplo ad_dir.$file))
."</font></small></small></sub>";
$filelist .="<br>";
}
}
function do_upload($uplo ad_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.$fi le_name;
if ( $file_name =="") {
$message = "Invalid File Name Specified";
return $message;
}
$result = move_uploaded_f ile($temp_name, $file_path);
if (!chmod($file_p ath,0777))
$message = "change permission to 777 failed.";
else
$message = ($result)?"$fil e_name uploaded successfully." :
"Somthing is wrong with uploading a file.";
return $message;
}
?>
<center>
<font color=red><?=$_ REQUEST[message]?></font>
<br>
<form name="upload" id="upload" ENCTYPE="multip art/form-data" method="post">
Upload File <input type="file" id="userfile" name="userfile" >
<input type="submit" name="upload" value="Upload">
</form>
<br><b>My Files</b>
<hr width=70%>
<?=$filelist? >
<hr width=70%>
<small><sup>Dev eloped By
<a style="text-decoration:none " href="http://tech.tailoredwe b.com">Tailored Web.com</a>
</sup></small>
</center>
[/PHP]