When i upload files via a php script (posted below) the scandir script doesn't find them immediately even though when i access the directory via ftp all the files are there. Looking for guidance as to where the problem might be...perhaps with server settings rather then the php code?
Code: Select all
define ("MAX_SIZE","100");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) {
return "";
}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if(isset($_POST['Submit'])) {
//reads the name of the file the user submitted for uploading
echo ("<p>form submitted</p>");
$image=$_FILES['image']['name'];
echo ("<p>Original File Name: ".$image."</p>");
$chris_name=$_POST['newfilename'];
$chris_name2=$_POST['newfilename2'];
$chris_name3=$_POST['newfilename3'];
echo ("<p>New File Name: ".$chris_name."-".$chris_name2."-".$chris_name3."</p>");
//if it is not empty
if ($image) {
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image']['name']);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
//print error message
echo '<h1>Unknown extension!</h1>';
$errors=1;
}
else {
//get the size of the image in bytes
$size=filesize($_FILES['image']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024){
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
$image_name= $chris_name.'-'.$chris_name2.'-'.$chris_name3.'.'.$extension;
$newname="../images/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}
}
}
}
//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors) {
echo "<h1>File Uploaded Successfully! Try again!</h1>";
}
?>
<form name="newad" method="post" enctype="multipart/form-data" action="">
<table>
<tr>
<td><input name="newfilename" type="text" value="newname">
</td><td>-</td>
<td><input name="newfilename2" type="text" value="newname2">
</td><td>-</td>
<td><input name="newfilename3" type="text" value="newname3">
</td>
<td><input type="file" name="image"></td></tr>
<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
</table> </form>