Markjose
Markjose

Reputation: 73

file upload return to upload html page

I'm looking to return to the previous page after a file upload and have "file uploaded successfully" on the upload page.

In upload.php at the top I have placed

sesssion_start();

And at the end of the file upload script I have placed

$_SESSION['upload_success'] = TRUE;
header("Location: stream.php");

Now I know i need to put some code into the html document but unsure what needs to go in. Below is my html form script

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="90000000" />
 Select video to upload:
Please choose a file: <input name="uploadedfile" type="file" /><br /> 
<input type="submit" value="Upload File" /> 

I know it is going to be something similar to this but unsure how or where I would place it.

 session_start();
 if (isset($_SESSION['upload_success']) && $_SESSION['upload_success']) {
 echo "File uploaded successfully";
 }

If someone could walk me through adding the HTML code into the correct place I will be very greatful

After the comments i amend my php code to look like this.

<?php error_reporting(E_ALL); ini_set('display_errors', 1);
sesssion_start();
$target_path = "upload/";
$target = $target_path . basename($_FILES['uploadedfile']['name'] );
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'] , $target))
{ 
echo "The file ". basename( $_FILES['uploadedfile']['name'] ). " has been      uploaded"; 
  } 
 else {
   echo "Sorry, there was a problem uploading your file."; 
    }
   $_SESSION['upload_success'] = TRUE;
   header("Location: stream.php");
   exit();  

And the syntax inside the stream.php to:

    <?phpsession_start();
    if (isset($_SESSION['upload_success']) && $_SESSION['upload_success']) {
    echo "File uploaded successfully";
    }
    ?>

Thanks,

Mark

Upvotes: 3

Views: 2225

Answers (4)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Nota: You also cannot use echo and header together because that would considered as outputting before header, so we'll just use a session array as the message and the header to redirect to "upload_form.php", then show the respective message on that page afterwards.

Use session_destroy() also to destroy any previous sessions.

Sidenote: Use two seperate files.

HTML form: call this "upload_form.php"

<?php 
session_start();
session_destroy();
?>

<form action="stream.php" method="post" enctype="multipart/form-data">

<input type="hidden" name="MAX_FILE_SIZE" value="90000000" />
Select video to upload:
Please choose a file: <input name="uploadedfile" type="file" /><br />

 <input type="submit" value="Upload File">
 </form>

<?php 
if(isset($_SESSION['upload_success'])){
    echo $_SESSION['upload_success'];
}

else{
    echo "Please select a file.";
}

?>

PHP (file 2): call this "stream.php"

<?php 
session_start();

$target_path = "upload/";
$target = $target_path . basename($_FILES['uploadedfile']['name'] );

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'] , $target))
{
$_SESSION['upload_success'] = "File successfully uploaded.";
header("Location: upload_form.php");
exit;
  } 

 else {
$_SESSION['upload_success'] = "Sorry, there was a problem uploading your file.";

header("Location: upload_form.php");
exit;
    }

Edit:

Modify and add the following after if(move_uploaded_file...

if(isset($_FILES['uploadedfile']) && !empty($_FILES['uploadedfile'])){
   $target_path = "upload/";
   $target = $target_path . basename($_FILES['uploadedfile']['name']);
}

Upvotes: 1

Ryan Mortensen
Ryan Mortensen

Reputation: 2327

Placing

$_SESSION['upload_success'] = TRUE;
header("Location: stream.php");

At the end, I believe, would set true no matter what actually happened with the file's upload the reason being, there is not a condition being checked.

Unless the script has an exit command when it fails, it will eventually get to the part where it says: "Set the upload success as true and then go to stream.php" rather than saying, "If the upload is successful, set the upload success as true and then go to stream.php"

I would try:

    <?php 
error_reporting(E_ALL); ini_set('display_errors', 1);

session_start();

if($_FILES['uploadedfile']['size'] == 0)//In other words, if no file was selected.
    {
    $_SESSION['upload_success'] = 4;//File wasn't selected
    header("Location: stream.php");
    exit();
    }   

if(!file_exists('upload/' . basename($_FILES['uploadedfile']['name'])))
    {       
    $_SESSION['upload_success'] = (move_uploaded_file($_FILES['uploadedfile']['tmp_name'],'upload/' . basename($_FILES['uploadedfile']['name'])) ? 1 : 2);
    }
elseif(file_exists('upload/' . basename($_FILES['uploadedfile']['name'])))
    {
    $_SESSION['upload_success'] = 3;
    }

header("Location: stream.php");
exit(); 
?>

Now in stream.php where you have your if statement that displays the message do this instead:

<?php
session_start();

switch (@$_SESSION['upload_success']) {
    case 1:
        echo "File uploaded successfully";
        break;
    case 2:
        echo "Sorry, there was a problem uploading your file.";
        break;
    case 3:
        echo "A file with that name already exists!";
        break;
    case 4:
        echo "You must select a file to upload!";
        break;
}
unset($_SESSION['upload_success']);
 ?>//So if you reload stream.php yet another time no messages will be displayed again for no reason. ie. none of the cases will match an unset variable.

Last, you cannot echo (or do any type of output meant to be viewed by a user) before you header(Location: "somepage.php");

The page will switch before the user can read the output.

The way your code is currently written in your question you could have the following happen:

  1. The server echos "Sorry, there was a problem uploading your file", which will never be seen by the user.
  2. $_SESSION['upload_success'] is then set to TRUE, which is obviously not in agreement with #1.
  3. It then sends the user to stream.php where a success message is displayed.

An alternate, lazier way with less useful scenario descriptions to also fix your problem would be to do this instead (in upload.php):

else 
    {
    die("Sorry, there was a problem uploading your file."); 
    }

Hope that helps!

Upvotes: 0

cssyphus
cssyphus

Reputation: 40038

For a quick solution, you could use Ravi Kusuma's jQuery File Upload Plugin or an AJAX solution to do this.

Another alternative, though, to those proposed above is to programmatically construct / output an HTML form with some javascript, and get it to POST a message to stream.php:

CAVEAT: I haven't tried this myself, but I can't think why it wouldn't work. Would someone please confirm my sanity? -- Tested it myself: it works.

<?php
    //upload.php

    //Do file upload stuff, then:

    $out = '
        <form id="frmUpOkay" action="stream.php" method="post">
            <input name="upMsg" value="Upload Successful" />
        </form>
        <script type="text/javascript">
            $(function(){
                $("#frmUpOkay").submit();
            });
        </script>
    ';
    echo $out;
?>

You must also add this bit to the top of the stream.php file:

<?php
    if ( isset($_POST['upMsg']) && isset($_POST['upMsg']) != '' ){
        $upMsg = $_POST['upMsg']; //you should sanitize this input
    }else{
        $upMsg = '';
    }
?>
<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
    <div>
        Your normal website content is here.<br>
        <br>
        Upload message: <?php echo $upMsg; ?> <br>
        <br>
    </div>
</body>

Notes:

Above code uses jQuery, so you would need the jQuery library included on your upload.php page (as shown above).

Upvotes: 0

Majid Ramzani
Majid Ramzani

Reputation: 369

Your code works fine, but you should remove session['upload_success'] with unset function after you do echo success message. try

 unset( $_SESSION['upload_success'])

in stream.php right after

 echo "File uploaded successfully";

update : if you want to work all these on a single page, You can simply do it like below:

 if(isset($_SESSION['upload_success']) and $_SESSION['upload_session'])
   {
//echo success message
//remove session
}
if(isset($_POST['file'])){
//upload process , if it was successfull make seesion true...

}
else {

//show form

}

Upvotes: 0

Related Questions