demon_athens Posted May 22, 2007 Share Posted May 22, 2007 I have this code <?php $imagename = $_GET['imagename']; $renjpg = $iname[0].".jpg"; rename("../../UserFiles/Image/layouts/$imagename" , "../../UserFiles/Image/layouts/$renjpg"); // echo "<br> RENAMED PIC"; // echo "<br>image = ".$imagename; // echo "<br>NEW image = ".$renjpg."<br>"; ?> There is something wrong with my syntax in the rename, because I can't rename it! I tried a dozen things but couldn't find how to write the $iname[0].".jpg".. Please help me, I've spend hours the last days trying to find out what the right syntax will be. I am stuck to this thing Quote Link to comment Share on other sites More sharing options...
demon_athens Posted May 22, 2007 Author Share Posted May 22, 2007 There is something wrong with php5 on a pc. The same script works on 4.3 and Linux. So definetely the syntax is wrong... Any thoughts? Quote Link to comment Share on other sites More sharing options...
Silverado_NL Posted May 22, 2007 Share Posted May 22, 2007 when running it on php5 what os are you using???? if its windows, the directory structure differs from linux instead of rename("../../UserFiles/Image/layouts/$imagename" , "../../UserFiles/Image/layouts/$renjpg"); try using rename("..\..\UserFiles\Image\layouts\$imagename" , "..\..\UserFiles\Image\layouts\$renjpg"); greetz Silverado Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 22, 2007 Share Posted May 22, 2007 When using PHP on Windows, you can use forward slashes in the directory string with no problems. PHP takes care of the translation. Ken Quote Link to comment Share on other sites More sharing options...
demon_athens Posted May 22, 2007 Author Share Posted May 22, 2007 thank you Silverado_NL for your concern, Although I was sure that the slashes had nothing to do with my issue i did what you said and nothing changed. When I have something simple like this works : <?php $imagename = "test.JPG"; $renjpg = "renamed_test.jpg"; rename("../../UserFiles/Image/layouts/$imagename" , "../../UserFiles/Image/layouts/$renjpg"); // echo "<br> RENAMED PIC"; // echo "<br>image = ".$imagename; // echo "<br>NEW image = ".$renjpg."<br>"; ?> So my conclusion was that the syntax has a problem. Maybe my php.ini need modification or in PHP5.0 there is an alternative way to write this simple thing. Even if the thing with slashes worked it would be a problem because when i finish it I'll upload it to a linux server so I must rewrite/change those lines again. I am really confused. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 22, 2007 Share Posted May 22, 2007 So you're problem is with this line: <?php $renjpg = $iname[0].".jpg"; ?> Where is the array $iname being set? Ken Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 22, 2007 Share Posted May 22, 2007 try this. it may releave something change <?php rename("../../UserFiles/Image/layouts/$imagename" , "../../UserFiles/Image/layouts/$renjpg"); ?> to <?php $fFrom = "../../UserFiles/Image/layouts/$imagename"; $fTo = "../../UserFiles/Image/layouts/$renjpg"; echo "from: $fFrom<br>to: $fTo"; rename($fFrom , $fTo); ?> post the from: and to: on the results that fail (also one what that works) it also could be directory structor Quote Link to comment Share on other sites More sharing options...
demon_athens Posted May 22, 2007 Author Share Posted May 22, 2007 Yes Ken, this line is causing the problem. The $iname is the result of an explode of a get variable that has a full filename. In the code below you can see what I mean I also tried this and again didn't work <?php // get the variable - its a $_GET thing in real life $imagename1="TEST.JPG"; $iname2 = explode(".",$imagename1); //get the filename without extension $renjpg = $iname2[0].".jpg"; // add the extension .jpg // $renjpg = "TEST.JPGI"; WHEN THIS LINE IS UN commented it works! echo "original picture ".$imagename." renamed to ".$renjpg; //check if imagename exist in this path if(file_exists($imagename)) echo "<p>FILE EXIST</p>"; //rename($imagename , $renjpg); rename("$imagename1" , "$renjpg"); if(rename($imagename1 , $renjpg)) {echo "<br><br>DONE!";}else{echo "<br><br>PROBLEM";} ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 22, 2007 Share Posted May 22, 2007 You're trying to do the rename twice, so the second one fails: <?php rename("$imagename1" , "$renjpg"); if(rename($imagename1 , $renjpg)) {echo "<br><br>DONE!";}else{echo "<br><br>PROBLEM";} ?> remove the first "rename". Ken Quote Link to comment Share on other sites More sharing options...
demon_athens Posted May 22, 2007 Author Share Posted May 22, 2007 thanks, my mistake. bust still nothing changed. I uploaded to the unix server http://www.lollypop.gr/test/test.php I can get it, I am going crazy. how the hell can I write the $iname2[0].".jpg" ? Quote Link to comment Share on other sites More sharing options...
demon_athens Posted May 22, 2007 Author Share Posted May 22, 2007 if someone could copy paste that script to its PC with a TEST.JPG file in the same path and tell me if works for him, would help. thank you very much. Quote Link to comment Share on other sites More sharing options...
corbin Posted May 22, 2007 Share Posted May 22, 2007 Works perfect for me on Win XP MCE with Apache 2.2 and PHP 5.2.... Quote Link to comment Share on other sites More sharing options...
demon_athens Posted May 23, 2007 Author Share Posted May 23, 2007 any ideas what can block it in my php.ini or apache settings? Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted May 23, 2007 Share Posted May 23, 2007 that won't/ or shouldn't make a difference with Apache at all. Quote Link to comment Share on other sites More sharing options...
demon_athens Posted May 23, 2007 Author Share Posted May 23, 2007 After a lot of searching and tests I think that I found it. I recopy the "working" script <?php // get the variable - its a $_GET thing in real life $imagename="TEST.JPG"; $iname2 = explode(".",$imagename); //get the filename without extension $renjpg = $iname2[0].".jpg"; // add the extension .jpg $tempjpg = "temp.jpg"; // $renjpg = "TEST.JPGI"; WHEN THIS LINE IS UN commented it works! //check if imagename exist in this path //if(file_exists($imagename1)) echo "<p>FILE EXIST</p>"; rename($imagename,$renjpg); ?> This code DOESN'T WORK on windows. The reason is because windows are case Insesitive, so when you want to change TEST.JPG to TEST.jpg, the execution happens but the file system seems intact because for windows ".jpg" and ".JPG" is the same. The above code works on a linux server. Also found those information: rename() definitely does not follow the *nix rename convention on WinXP with PHP 5. If the $newname exists, it will return FALSE and $oldname and $newname will remain in their original state. You can do something like this instead: function rename_win($oldfile,$newfile) { if (!rename($oldfile,$newfile)) { if (copy ($oldfile,$newfile)) { unlink($oldfile); return TRUE; } return FALSE; } return TRUE; } There are also many more on php.net about the issue of rename function under Windows. ( try searching about rename()) Any thoughts or concerns would be great. My script will not work locally but currently its fine by me because the final script will be uploaded to a linux server. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.