Jump to content

How do I change where my php file saves a file?


Go to solution Solved by Psycho,

Recommended Posts

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

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.

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.

  • Solution

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!';
    }
}

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 24

Warning: 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 25

Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/content/85/9477285/html/uniqueminecraftservers/upload/upload.php on line 26

Warning: 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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.