Caitlin-havener Posted June 24, 2012 Share Posted June 24, 2012 I'm working on wamp local server trying to rename some files with the following code: <?php$file=fopen("img/emvivacesupplemental2012/filelist.txt","r");$workingDir=getcwd(); $directory= "\\img\\emvivacesupplemental2012\\450pxw\\";$newDirectory="/img/emvivacesupplemental2012/gallery/"; $allFiles[][]= array(); while (!feof($file)){/*$fileLine=$workingDir.$directory.fgets($file);if (file_exists($fileLine)){ echo ($fileLine . "exists <br/>");}else{ echo ($fileLine. "does NOT exist </br>");}*/ for($i=1000;$i<=9999;$i++) { //if first four characters if ($i==substr($fileLine,0,4)) { $allFiles[$i][]=$fileLine; foreach ($allFiles as $v1) { foreach ($v1 as $key => $v2) { rename($directory.$fileLine, $newDirectory.$i."_gallery_".$key.".jpg"); } } } else if ($i==substr($fileLine,1,4)) { $firstChar=substr($fileLine,0,1); $allFiles[$firstChar.$i][]=$fileLine; foreach ($allFiles as $v1) { foreach ($v1 as $key => $v2) { rename($directory.$fileLine, $newDirectory.$firstChar.$i."_gallery_".$key.".jpg"); } } } else if ($i==substr($fileLine,2,4)) { $allFiles[$i][]=$fileLine; foreach ($allFiles as $v1) { foreach ($v1 as $key => $v2) { rename($directory.$fileLine, $newDirectory.$i."_gallery_".$key.".jpg"); } } } else if ($i==substr($fileLine,3,4)) { $firstChar=substr($fileLine,2,1); $allFiles[$i][]=$fileLine; foreach ($allFiles as $v1) { foreach ($v1 as $key => $v2) { rename($directory.$fileLine, $newDirectory.$firstChar.$i."_gallery_".$key.".jpg"); } } } }}print_r($allFiles);fclose($file);?> I ran file_exists function on each line and nothing I do is yielding a boolean yes. I've tried preceding with the working directory, both forward and backslashes.. the images ARE in the right directory. Copied straight from properties of one of the images : C:\wamp\www\alluringassets\img\emvivacesupplemental2012\450pxw Link to comment Share on other sites More sharing options...
birbal Posted June 24, 2012 Share Posted June 24, 2012 (edited) i have no idea what are you trying to do actually. if ($i==substr($fileLine,0,4)) { $allFiles[$i][]=$fileLine; foreach ($allFiles as $v1) { foreach ($v1 as $key => $v2) { rename($directory.$fileLine, $newDirectory.$i."_gallery_".$key.".jpg"); }where does your $fileLine is defined? condition probably even are not matching so thus if part is never executing. try to add some debug statements or try to print return value of substr() to be sure. echo out the destination file and renamed file before you use rename() to see what is evaluating Edited June 24, 2012 by birbal Link to comment Share on other sites More sharing options...
Caitlin-havener Posted June 24, 2012 Author Share Posted June 24, 2012 (edited) Forget those if statements for a moment. This is what I need to figure out. The following code is echoing does not exist for all the files. I need to first have them echoing that they do exist. $file=fopen("img/emvivacesupplemental2012/filelist.txt","r");$workingDir=getcwd(); $directory= "\\img\\emvivacesupplemental2012\\450pxw\\";$newDirectory="/img/emvivacesupplemental2012/gallery/"; $allFiles[][]= array(); while (!feof($file)){$fileLine=$workingDir.$directory.fgets($file);if (file_exists($fileLine)){ echo ($fileLine . "exists <br/>");}else{ echo ($fileLine. "does NOT exist </br>");}}?> Example of response: C:\wamp\www\alluringassets\img\emvivacesupplemental2012\450pxw\8518_b.jpg does NOT exist C:\wamp\www\alluringassets\img\emvivacesupplemental2012\450pxw\8518_f.jpg does NOT exist C:\wamp\www\alluringassets\img\emvivacesupplemental2012\450pxw\8551_b.jpg does NOT exist C:\wamp\www\alluringassets\img\emvivacesupplemental2012\450pxw\8551_f.jpg does NOT exist C:\wamp\www\alluringassets\img\emvivacesupplemental2012\450pxw\8653_b.jpg does NOT exist C:\wamp\www\alluringassets\img\emvivacesupplemental2012\450pxw\8653_f.jpg does NOT exist C:\wamp\www\alluringassets\img\emvivacesupplemental2012\450pxw\8654_b.jpg does NOT exist C:\wamp\www\alluringassets\img\emvivacesupplemental2012\450pxw\8654_f.jpg does NOT exist C:\wamp\www\alluringassets\img\emvivacesupplemental2012\450pxw\8655_b.jpg does NOT exist Those are the exact paths! I've also tried it without the working directory preceding it. Which would then read "\img\emvivacesupplemental\450pxw\8518_b.jpg does NOT exist!" I've also tried forward slashes instead of backslashes. All echo "does not exist". Edited June 24, 2012 by Caitlin Link to comment Share on other sites More sharing options...
boen_robot Posted June 24, 2012 Share Posted June 24, 2012 It could be access issue. Try to verify all folders in the path: echo (int) file_exists('C:');echo (int) file_exists('C:\wamp');echo (int) file_exists('C:\wamp\www');echo (int) file_exists('C:\wamp\www\alluringassets');echo (int) file_exists('C:\wamp\www\alluringassets\img');echo (int) file_exists('C:\wamp\www\alluringassets\img\emvivacesupplemental2012');echo (int) file_exists('C:\wamp\www\alluringassets\img\emvivacesupplemental2012\450pxw'); If all is OK, this should output "111111". The first folder where it goes to "0" is the problematic one. On that folder, you'll have to right click, select "Properties", and from the security tab, allow PHP to have access. One foolproof way is to give full access to "Everyone". This isn't a good idea if your computer is running a public facing site, but if it's just a development copy, that's OK. Link to comment Share on other sites More sharing options...
Caitlin-havener Posted June 24, 2012 Author Share Posted June 24, 2012 It could be access issue. Try to verify all folders in the path:echo (int) file_exists('C:');echo (int) file_exists('C:\wamp');echo (int) file_exists('C:\wamp\www');echo (int) file_exists('C:\wamp\www\alluringassets');echo (int) file_exists('C:\wamp\www\alluringassets\img');echo (int) file_exists('C:\wamp\www\alluringassets\img\emvivacesupplemental2012');echo (int) file_exists('C:\wamp\www\alluringassets\img\emvivacesupplemental2012\450pxw'); If all is OK, this should output "111111". The first folder where it goes to "0" is the problematic one. On that folder, you'll have to right click, select "Properties", and from the security tab, allow PHP to have access. One foolproof way is to give full access to "Everyone". This isn't a good idea if your computer is running a public facing site, but if it's just a development copy, that's OK. It's yielding all 1's!! Do I still need to change permissions?? Link to comment Share on other sites More sharing options...
boen_robot Posted June 24, 2012 Share Posted June 24, 2012 And echo (int) file_exists('C:\wamp\www\alluringassets\img\emvivacesupplemental2012\450pxw\8518_b.jpg'); is still giving 0, yet you're sure it exists in that folder, and is called like that (and not, say ".JPG" or ".jpeg")?You may need to adjust the permissions on the file itself then. Link to comment Share on other sites More sharing options...
Caitlin-havener Posted June 24, 2012 Author Share Posted June 24, 2012 Someone else helped me on facebook. I actually had to use trim function ! Will let you know if I have trouble with this next part shortly Link to comment Share on other sites More sharing options...
Caitlin-havener Posted June 24, 2012 Author Share Posted June 24, 2012 Kay im still getting this error with and without the working directory specified: Warning: rename(C:\wamp\www\alluringassets\img\emvivacesupplemental2012\450pxw\8518_b.jpg,C:\wamp\www\alluringassets\img\emvivacesupplemental2012\gallery\8518_gallery_0.jpg) [function.rename]: The system cannot find the file specified. (code: 2) in C:\wamp\www\alluringassets\renamegalleryimgs.php on line 20 while (!feof($file)){$fileLine=trim(fgets($file)); for($i=1000;$i<=9999;$i++) { //if first four characters if ($i==substr($fileLine,0,4)) { $allFiles[$i][]=$fileLine; foreach ($allFiles as $v1) { foreach ($v1 as $key => $v2) { rename($workingDir.$directory.$fileLine, $workingDir.$newDirectory.$i."_gallery_".$key.".jpg"); } } } Link to comment Share on other sites More sharing options...
boen_robot Posted June 24, 2012 Share Posted June 24, 2012 The folder that you place a file into must exist prior to the renaming, so if you don't already have the "gallery" folder, create it first. You can use mkdir() to create it automatically. Link to comment Share on other sites More sharing options...
Caitlin-havener Posted June 24, 2012 Author Share Posted June 24, 2012 I've already made the gallery folder... next... lol. Still errors mehh Link to comment Share on other sites More sharing options...
Caitlin-havener Posted June 25, 2012 Author Share Posted June 25, 2012 Text file attached. Current code, below. Some files were renamed, others disappeared into the twilight zone. <?php$file=fopen("img/emvivacesupplemental2012/filelist.txt","r");$workingDir=getcwd();$directory= "\\img\\emvivacesupplemental2012\\450pxw\\";$newDirectory="\\img\\emvivacesupplemental2012\\gallery\\";$allFiles[][]= array();while (!feof($file)){$fileLine=trim(fgets($file));var_dump(scandir($workingDir.$directory));//echo $workingDir.$directory.$fileLine." is...<br/>"; //var_dump(file_exists($workingDir.$directory.$fileLine)); for($i=1000;$i<=9999;$i++) { //if first four characters if ($i==substr($fileLine,0,4)) { $allFiles[$i][]=$fileLine; foreach ($allFiles as $v1) { foreach ($v1 as $key => $v2) { rename($workingDir.$directory.$fileLine, $workingDir.$newDirectory.$i."_gallery_".$key.".jpg"); } } } else if ($i==substr($fileLine,1,4)) { $firstChar=substr($fileLine,0,1); $allFiles[$firstChar.$i][]=$fileLine; foreach ($allFiles as $v1) { foreach ($v1 as $key => $v2) { rename($workingDir.$directory.$fileLine, $workingDir.$newDirectory.$firstChar.$i."_gallery_".$key.".jpg"); } } } else if ($i==substr($fileLine,2,4)) { $allFiles[$i][]=$fileLine; foreach ($allFiles as $v1) { foreach ($v1 as $key => $v2) { rename($workingDir.$directory.$fileLine, $workingDir.$newDirectory.$i."_gallery_".$key.".jpg"); } } } else if ($i==substr($fileLine,3,4)) { $firstChar=substr($fileLine,2,1); $allFiles[$i][]=$fileLine; foreach ($allFiles as $v1) { foreach ($v1 as $key => $v2) { rename($workingDir.$directory.$fileLine, $workingDir.$newDirectory.$firstChar.$i."_gallery_".$key.".jpg"); } } } }}//print_r($allFiles);fclose($file);?> filelist.txt Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now