guevarak12
guevarak12

Reputation: 75

php file uploader not uploading files to my site

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?

Upvotes: 0

Views: 218

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

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>

Upvotes: 1

Brian Dillingham
Brian Dillingham

Reputation: 9356

<?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

?>

Upvotes: 1

Related Questions