0

I have this html form

<html>
<body>
    <form action="accept-file.php" method="post" enctype="multipart/form-data">
        Your Photo: <input type="file" name="photo" size="25" />
        <input type="submit" name="submit" value="Submit" />
    </form>
</body>

and this php file

<?php
if($_FILES['photo']['name'])
{
    if(!$_FILES['photo']['error'])
    {
        $new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
        if($_FILES['photo']['size'] > (10240000)) 
        {
            $valid_file = false;
            $message = 'Your file is to large.';
        }

        if($valid_file)
        {
            move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);
            $message = 'Congratulations!  Your file was accepted.';
        }
    }
    else
    {
        $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
    }
}

$_FILES['field_name']['name']
$_FILES['field_name']['size']
$_FILES['field_name']['type']
$_FILES['field_name']['tmp_name']
 ?>

I get my uploader working, but the accept-file.php does not diplay the image on mysite/accept-file.php. How can I get my picture to display on accept-file.php?

6
  • "but the accept-file.php does not diplay the image on mysite/accept-file.php" - $_FILES['field_name']['name'] $_FILES['field_name']['size'] $_FILES['field_name']['type'] $_FILES['field_name']['tmp_name'] these need ending semi-colons, but you're not echoing anything. Having used error_reporting(E_ALL); ini_set('display_errors', 1); would have triggered a parse error. Commented Aug 26, 2014 at 22:45
  • @Fred-ii- I think PHP should even throw an error because of this. Commented Aug 26, 2014 at 22:46
  • @CharlotteDunois Most likely OP's error reporting isn't set/on, and/or isn't checking for them, or didn't mention it. Commented Aug 26, 2014 at 22:47
  • @Fred-ii- And that's why they should get hit. Commented Aug 26, 2014 at 22:48
  • my accepted-file.php page is blank Commented Aug 26, 2014 at 22:48

2 Answers 2

1
<?php

// added && !$_FILES['photo']['error'] to top if block to make it clearer
// removed $valid_file = false;
// changed from if($valid_file) because it wasn't set and is valid 
// outputted image & message at the bottom

if($_FILES['photo']['name'] && !$_FILES['photo']['error'])
{ 

    if($_FILES['photo']['size'] > (10240000)) 
    {
        $message = 'Your file is to large.';
    }

    else
    { 
         move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$_FILES['photo']['name']); // changed to name, because tmp_name has no extension 
         $message = 'Congratulations!  Your file was accepted.';
    }
}
else
{
    $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
}


echo "<h1>$message</h1>"; // output message
echo '<img src="uploads/'.$_FILES['photo']['name'].'"/>'; // output image from server

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Just a quick note: echo '<img src="'.$_FILES['photo']['name'].'"/>'; should read as echo '<img src="uploads/'.$_FILES['photo']['name'].'"/>'; otherwise, the image won't echo in the browser afterwards.
1

Here, give this a go, and make sure the uploads folder has proper write permissions and run this script from the root of your server.

<?php

$folder = "uploads/";

if($_FILES['photo']['name'])
{
    if(!$_FILES['photo']['error'])
    {

        if($_FILES['photo']['size'] > (10240000)) 
        {
            $message = 'Your file is to large.';
        }

        else
        {
            move_uploaded_file($_FILES['photo']['tmp_name'], $folder.$_FILES['photo']['name']);
            $message = 'Congratulations!  Your file was accepted.';
        }
    }

        else {
        $message = 'Ooops! Your upload triggered the following error: '.$_FILES['photo']['error'];
        }
}

        echo "<b>$message</b>";

        $name = "File name: " . $_FILES['photo']['name'];
        $size = "File size: " . $_FILES['photo']['size'];
        $type = "File type: " . $_FILES['photo']['type'];
        $tmp  = "File tmp: " . $_FILES['photo']['tmp_name'];

        $showdetails = "<hr>" . $name . "<br>" . $size . "<br>" . $type . "<br>" . $tmp . "<br>";

        echo $showdetails;


        echo '<img src="$folder'.$_FILES['photo']['name'].'"/>';


?>

<style>
body{
font-family:Georgia;
}
</style>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.