Jump to content

Upload Image - Enhancing Script


matkins70

Recommended Posts

Hi. A friend gave me a script he wrote to upload Jpgs and Gifs quickly to his site, without having to log in, or ftp. I added a bit on to upload png, so it would serve a purpose for me. But what I would like now, is it to display the picture and URL after it has succesfully uploaded, so that I can make sure the picture is correct, and then have the URL at hand, so I can use it.

I tried fiddling with it a bit, but didn't get anywhere, so here is the original script:



[code]<?php # Script 11.1 - upload_image.php

// Check if the form has been submitted.
if (isset($_POST['submitted'])) {

    // Check for an uploaded file.
    if (isset($_FILES['upload'])) {
       
        // Validate the type. Should be jpeg, jpg, png, or gif.
        $allowed = array ('image/gif', 'image/jpeg', 'image/jpg', 'image/pjpeg', 'image/png');
        if (in_array($_FILES['upload']['type'], $allowed)) {
       
            // Move the file over.
            if (move_uploaded_file($_FILES['upload']['tmp_name'], "{$_FILES['upload']['name']}")) {
           
                echo '<p>The file has been uploaded!</p>';
               
           
            } else { // Couldn't move the file over.
           
                echo '<p><font color="red">The file could not be uploaded because: </b>';
       
                // Print a message based upon the error.
                switch ($_FILES['upload']['error']) {
                    case 1:
                        print 'The file exceeds the upload_max_filesize setting in php.ini.';
                        break;
                    case 2:
                        print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
                        break;
                    case 3:
                        print 'The file was only partially uploaded.';
                        break;
                    case 4:
                        print 'No file was uploaded.';
                        break;
                    case 6:
                        print 'No temporary folder was available.';
                        break;
                    default:
                        print 'A system error occurred.';
                        break;
                } // End of switch.
               
                print '</b></font></p>';

            } // End of move... IF.
           
        } else { // Invalid type.
            echo '<p><font color="red">Please upload a JPEG, PNG or GIF image.</font></p>';
            unlink ($_FILES['upload']['tmp_name']); // Delete the file.
        }

    } else { // No file uploaded.
        echo '<p><font color="red">Please upload a JPEG, PNG or GIF image smaller than 512KB.</font></p>';
    }
           
} // End of the submitted conditional.
?>
   
<form enctype="multipart/form-data" action="addpicture.php" method="post">

    <input type="hidden" name="MAX_FILE_SIZE" value="524288">
   
    <fieldset><legend>Select a JPEG, PNG or GIF image to be uploaded:</legend>
   
    <p><b>File:</b> <input type="file" name="upload" /></p>
   
    </fieldset>
    <div align="center"><input type="submit" name="submit" value="Submit" /></div>
    <input type="hidden" name="submitted" value="TRUE" />
</form>[/code]


I would have thought you would do something with the line:
[code]
echo '<p>The file has been uploaded!</p>';[/code]


but just kept getting errors.

Is this the way to go, or would it be better to have a succesful page, and have it displayed there?

Thanks
Link to comment
https://forums.phpfreaks.com/topic/27644-upload-image-enhancing-script/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.