Jump to content

Download fail message.


klturi421

Recommended Posts

What I am trying to achieve here is a simple URL to server file download script. It works fantastically, however, I am trying to achieve an error message when the script fails to get a file. Right now, when the file is downloaded, it provides the echo 'Successfully downloaded (FILENAME) !'. But when it fails I would like for it to say something like, file could not be downloaded.

 

As of right now it gives me a warning that it could not open stream and it still echos file successfully downloaded. Any advice?

 

Full code:

<?php
include '../dbc.php';
page_protect();

if (isset($_POST['submit'])) {
$file = $_POST['filename'];
$target= $_POST['hosttarget'];
file_put_contents("$file", file_get_contents("$target"));
echo "Successfully uploaded ".$file."!";
} else{
	echo "Unable to retrieve file!";
	}
?>
<div class="wrap">
<FORM ENCTYPE="multipart/form-data" ACTION="" METHOD=POST>
<div class="download">
<div class="left">Target:<br>Filename:</div>
<div class="right"><INPUT TYPE="text" NAME="hosttarget" ID="text" VALUE=""><br>
	 <INPUT TYPE="text" NAME="filename" ID="text" VALUE=""><br></div>


	<INPUT TYPE="submit" NAME="submit" ID="submit " CLASS='submit_btn' VALUE="Get File">
	<div class="clear"></div>
</div>
</FORM>
</div>

Link to comment
Share on other sites

couple of things here, first I will write the correct code and then I will explain.

 

if (isset($_POST['submit'])) {
$file = $_POST['filename'];
$target= $_POST['hosttarget'];
if(file_put_contents($file, file_get_contents($target)) !== false) {
      echo "Successfully uploaded {$file}!";
} else{
	echo "Unable to retrieve file!";
        }
}

 

your previous code only checked to see if $_POST['submit'] was set, but even if it is set, that does not mean that retrieving the filename was successful, therefore your conditional would always return true even if the file_put_contents() returned false. Also, you do not need quotes around $target or $file, since they are variables and already contain quotes.

Link to comment
Share on other sites

Awesome! That helps out a lot!

 

When I put in a bad URL it returns the echo but also returns two warning messages about opening the stream. Is it possible to have it just return the echo and not the warnings?

 

I found the following code which works well. I prefer to have everything working with no warnings unless something actually is not working right besides a bad URL.

 

 // Turn off all error reporting
error_reporting(0);

 

If not, I am comfortable with the warnings, I just prefer a prettier end result. :) 

 

 

Link to comment
Share on other sites

you want the errors to be sent to the console log, however you do not want them to be sent to the user obviously, to do this you can turn display errors off, which will still send errors to the console for you to view but users will only see the error that you output.

 

ini_set("display_errors","no");

Link to comment
Share on other sites

That script is very dangerous. You should never use data from the user without sanitizing it first especially when you are using it to access/read files. Anyway, you just need to check the result of each process that performs the reads/writes and provide an appropriate error message when there is a failure. You can suppress the error messages returned by PHP using the '@' symbol before the function.

 

<?php

include '../dbc.php';
page_protect();

$result = '';
if (isset($_POST['submit']))
{
    $file   = $_POST['filename'];
    $target = $_POST['hosttarget'];

    if(!($file_contents = @file_get_contents($target)))
    {
        $result = "ERROR: Unable to retrieve file!";
    }
    elseif(!($file_write = @file_put_contents($file, $file_contents)))
    {
        $result = "ERROR: Uunable to create new file!";
    }
    else
    {
        $result = "Successfully uploaded {$file}!";
    }
}

?>
<html>
<body>

<div id="result"><?php echo $result; ?></div>

<div class="wrap">
    <form enctype="multipart/form-data" action="" method="post">
    <div class="download">
        <div class="left">Target:<br>Filename:</div>
        <div class="right">
            <input type="text" name="hosttarget" id="text" value=""><br>
            <input type="text" name="filename" id="text" value=""><br>
            <input type="submit" name="submit" id="submit " class='submit_btn' value="Get File">
        </div>
        <div class="clear"></div>
   </div>
    </form>
</div>

</body>
</html>

Link to comment
Share on other sites

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.