scandir not finding recently uploaded file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crispy_bcn
Forum Newbie
Posts: 1
Joined: Tue Jan 25, 2011 1:38 pm

scandir not finding recently uploaded file

Post by crispy_bcn »

I use scandir to get a list of files in my images directory and all works perfectly except in one situation.
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>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: scandir not finding recently uploaded file

Post by AbraCadaver »

Try clearstatcache() before running scandir(). scandir() isn't listed as an affected function but it's worth a shot. Failing that try glob() instead.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply