HeyAwesomePeople Posted June 20, 2013 Share Posted June 20, 2013 Right now I have a PHP code which takes a template, changes some things on it, and saves it to the same folder the .php file is in. How do I make it so that I can change where that file is saved? <?php if($_POST['submit']) { $name = htmlspecialchars($_POST['sName']); $ip = htmlspecialchars($_POST['sIp']); $port = htmlspecialchars($_POST['sPort']); $desc = htmlspecialchars($_POST['sDesc']); $finalName = $ip."(".$port.").html"; $writestring = ' <tr> <td class="trank">*</td> <td class="tname"><p class="wrap-tname"><a href="/server/5510">'.$name.'</a></p></td> <td class="tserver"><a href="/server/5510"><img style="border:none;width:468px;height:60px;" src="/uniqueminecraftservers/slist/banners/1.jpg"></a><br>IP: '.$ip.'</td> <td class="ttype"><p class="wrap-ttype">'.$port.'</p></td> <td class="tplayers">2078/3000 </td> <td class="tstatus Online">Online </td> </tr>'; if (file_exists($ip."(".$port.").html")) { echo 'File already exists'; } else { $finalName = $ip."(".$port.").html"; $file = fopen($finalName, "w"); $size = filesize($finalName); fwrite($file, $writestring); fclose($file); echo 'File Saved. Your server is now listed!'; } } $url = "http://www.maytagaclasvegas.com/uniqueminecraftservers/"; function redirect($url){ if (headers_sent()){ die('<script type="text/javascript">window.location.href="' . $url . '";</script>'); }else{ header('Location: ' . $url); die(); } } ?> Thanks! -HeyAwesomePeople Quote Link to comment https://forums.phpfreaks.com/topic/279396-how-do-i-change-where-my-php-file-saves-a-file/ Share on other sites More sharing options...
boompa Posted June 20, 2013 Share Posted June 20, 2013 Add the path here, before the file name: $finalName = $ip."(".$port.").html"; Make sure the directory to which you intend to write already exists, and the user under which PHP/the webserver is running has write permissions on that directory. Quote Link to comment https://forums.phpfreaks.com/topic/279396-how-do-i-change-where-my-php-file-saves-a-file/#findComment-1437108 Share on other sites More sharing options...
HeyAwesomePeople Posted June 20, 2013 Author Share Posted June 20, 2013 I changed $finalName = "$ip.(".$port.").html"; To $finalName = "/uniqueminecraftservers/slist/$ip.(".$port.").html"; Still uploads the file to the same folder AND now the check for if the file already exists is broken, it just rewrites it. Quote Link to comment https://forums.phpfreaks.com/topic/279396-how-do-i-change-where-my-php-file-saves-a-file/#findComment-1437110 Share on other sites More sharing options...
Solution Psycho Posted June 20, 2013 Solution Share Posted June 20, 2013 Seriously? You are defining $finalName in multiple places and aren't using it where you need to be. You first define it on line 9. Then on line 24 where you check for an existing file you don't use the variable at all and instead do a hard-coded check. Then you redefine the variable on line 28! Define it once and use it in all the places where you need it. Besides the variable you are creating is not whet you think it is - echo it to the page and see. if($_POST['submit']) { $name = htmlspecialchars($_POST['sName']); $ip = htmlspecialchars($_POST['sIp']); $port = htmlspecialchars($_POST['sPort']); $desc = htmlspecialchars($_POST['sDesc']); $finalName = "/uniqueminecraftservers/slist/{$ip}({$port}).html"; if (file_exists($finalName)) { echo 'File already exists'; } else { $writestring = ' <tr> <td class="trank">*</td> <td class="tname"><p class="wrap-tname"><a href="/server/5510">'.$name.'</a></p></td> <td class="tserver"><a href="/server/5510"><img style="border:none;width:468px;height:60px;" src="/uniqueminecraftservers/slist/banners/1.jpg"></a><br>IP: '.$ip.'</td> <td class="ttype"><p class="wrap-ttype">'.$port.'</p></td> <td class="tplayers">2078/3000</td> <td class="tstatus Online">Online</td> </tr>'; $file = fopen($finalName, "w"); $size = filesize($finalName); fwrite($file, $writestring); fclose($file); echo 'File Saved. Your server is now listed!'; } } Quote Link to comment https://forums.phpfreaks.com/topic/279396-how-do-i-change-where-my-php-file-saves-a-file/#findComment-1437111 Share on other sites More sharing options...
HeyAwesomePeople Posted June 20, 2013 Author Share Posted June 20, 2013 I already tried doing that, then I tried your code above. Both came out with many errors. Warning: fopen(/uniqueminecraftservers/slist/70.170.21.9(25565).php) [function.fopen]: failed to open stream: No such file or directory in /home/content/85/9477285/html/uniqueminecraftservers/upload/upload.php on line 24Warning: filesize() [function.filesize]: stat failed for /uniqueminecraftservers/slist/70.170.21.9(25565).php in /home/content/85/9477285/html/uniqueminecraftservers/upload/upload.php on line 25Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/content/85/9477285/html/uniqueminecraftservers/upload/upload.php on line 26Warning: fclose() expects parameter 1 to be resource, boolean given in /home/content/85/9477285/html/uniqueminecraftservers/upload/upload.php on line 27 If I kept it how it was, no errors would show and it would work fine, but the file would not go to the specified location. Quote Link to comment https://forums.phpfreaks.com/topic/279396-how-do-i-change-where-my-php-file-saves-a-file/#findComment-1437113 Share on other sites More sharing options...
HeyAwesomePeople Posted June 20, 2013 Author Share Posted June 20, 2013 FIXED I used /home/content/85/9477285/html/uniqueminecraftservers/slist/{$ip}({$port}).php INSTEAD OF JUST /uniqueminecraftservers/slist/{$ip}({$port}).php Thanks for your help guys! Quote Link to comment https://forums.phpfreaks.com/topic/279396-how-do-i-change-where-my-php-file-saves-a-file/#findComment-1437115 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.