Jump to content


Photo

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

php file location

Best Answer Psycho, 20 June 2013 - 03:01 PM

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!';
    }
}
Go to the full post


  • Please log in to reply
5 replies to this topic

#1 HeyAwesomePeople

HeyAwesomePeople

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 20 June 2013 - 02:36 PM

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



#2 boompa

boompa

    Advanced Member

  • Members
  • PipPipPip
  • 247 posts
  • LocationMassachusetts

Posted 20 June 2013 - 02:40 PM

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.



#3 HeyAwesomePeople

HeyAwesomePeople

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 20 June 2013 - 02:46 PM

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.



#4 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,814 posts
  • LocationCanada

Posted 20 June 2013 - 03:01 PM   Best Answer

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

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#5 HeyAwesomePeople

HeyAwesomePeople

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 20 June 2013 - 03:07 PM

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.



#6 HeyAwesomePeople

HeyAwesomePeople

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 20 June 2013 - 03:16 PM

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!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com