Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post

    Useful PHP Image Uploader

    Hey everyone! I just wanted to share my image uploading script I created. It takes an image (or multiple images), and uploads that picture to a folder, and then also generates a thumbnail and puts it in a separate folder. The destination folder is specified by the uploader before uploading images, and if the folder does not exist, then it creates it.

    upload.php
    Code:
    <!DOCTYPE html>
    <html lang="en-us">
    
    Upload images:
    
        <form action="upload_file.php" method="post" enctype="multipart/form-data">
            <input type="file" value="" name="upload_file[]" multiple>
            <br>Event Name: <input type="text" name="event">
            <br>Location: <input type="text" name="location"> (LEAVE BLANK IF ADDING TO AN EVENT ALREADY CREATED)
            <br>Year: <input type="text" name="year"> (LEAVE BLANK IF ADDING TO AN EVENT ALREADY CREATED)
            <br><button type="submit">Upload!</button>
        </form>
    
    </html>
    upload_file.php
    PHP Code:
    <?php
    include("config.php"); //includes database connection and defines security()

    function make_thumb($src$dest) {

        
    /* read the source image */
        
    $source_image imagecreatefromjpeg($src);
        
    $width imagesx($source_image);
        
    $height imagesy($source_image);
        
        
    /* find the "desired height" of this thumbnail, relative to the desired width  */
        
    $desired_width 65;
        
    $desired_height 65;
        
        
    /* create a new, "virtual" image */
        
    $virtual_image imagecreatetruecolor($desired_width$desired_height);
        
        
    /* copy source image at a resized size */
        
    imagecopyresampled($virtual_image$source_image0000$desired_width$desired_height$width$height);
        
        
    /* create the physical thumbnail image to its destination */
        
    imagejpeg($virtual_image$dest);
    }
    $event security($_POST['event']);
    $year security($_POST['year']);
    $location security($_POST['location']);


    if (
    file_exists($event)) {
        echo 
    "The folder already exists, not creating a new event <br />";
    } else {
        
    $sql "INSERT INTO table...";
        if(!
    $result $db->query($sql)){
            die(
    'There was an error running the query [' $db->error ']');
        } else {
            echo 
    "New event created <br />";
        }
    }


    $destFolder $event.'/';
    if (!
    file_exists($destFolder)) {
        
    mkdir($destFolder0777true);
        
    mkdir($destFolder.'pics/'0777true);
        
    mkdir($destFolder.'pics/thumbs/'0777true);
    }

    for(
    $i=0;$_FILES["upload_file"]["name"][$i]==true;$i++)
    {
        
    $fileName $_FILES["upload_file"]["name"][$i]; // The file name
        
    $fileTmpLoc $_FILES["upload_file"]["tmp_name"][$i]; // File in the PHP tmp folder
        
    $fileType = @$_FILES["upload_file"]["image/png||image/jpg"][$i];  // The type of file it is
        
    $fileSize $_FILES["upload_file"]["size"][$i]; // File size in bytes
        
    $fileErrorMsg $_FILES["upload_file"]["error"][$i]; // 0 = false | 1 = true
        
    $kaboom explode(".",$_FILES["upload_file"]["name"][$i]); // Split file name into an array using the dot
        
    $fileExt end($kaboom); // Now target the last array element to get the file extension
        
    $a 0
        
        
    $dir $destFolder.'pics/';
        if (
    $handle opendir($dir)) {
            while ((
    $file readdir($handle)) !== false){
                    if (!
    in_array($file, array('.''..')) && !is_dir($dir.$file)) 
                    
    $a++;
                }
        }

        
    $moveResultmove_uploaded_file($fileTmpLoc$destFolder"pics/" . ($a+1) . ".jpg");
        @
    make_thumb($destFolder."pics/".($a+1).".jpg"$destFolder."pics/thumbs/".($a+1).".jpg");



    }

    echo 
    "Images successfully uploaded!";

       


    ?>
    Last edited by markman641; 09-10-2013 at 05:11 AM.

  • #2
    0x3
    0x3 is offline
    New Coder
    Join Date
    Sep 2013
    Posts
    37
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Where's config.php?

  • #3
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    Quote Originally Posted by 0x3 View Post
    Where's config.php?
    you can create your own. you don't need it.

  • #4
    0x3
    0x3 is offline
    New Coder
    Join Date
    Sep 2013
    Posts
    37
    Thanks
    0
    Thanked 5 Times in 5 Posts
    '//includes database connection and defines security()'
    The security function is not defined anywhere else in that script, according to that comment. So if that is true, this script is useless as not everyone understands PHP.

  • #5
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    Quote Originally Posted by 0x3 View Post
    '//includes database connection and defines security()'
    The security function is not defined anywhere else in that script, according to that comment. So if that is true, this script is useless as not everyone understands PHP.
    it's not "useless". you have to take your own knowledge and make one simple function. if you don't know how to do that, then you shouldn't even be coding websites
    but for the noobs, i will edit the script

  • #6
    0x3
    0x3 is offline
    New Coder
    Join Date
    Sep 2013
    Posts
    37
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by markman641 View Post
    it's not "useless". you have to take your own knowledge and make one simple function. if you don't know how to do that, then you shouldn't even be coding websites
    but for the noobs, i will edit the script
    Do car manufactures release cars that are broken? No they release cars that are finished. No body will finish your script for you, its a custom function, it is in the package. This is not some big open source project that is well known, nor even a company project, so what would be the point in someone else finishing this?

  • #7
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    Quote Originally Posted by 0x3 View Post
    Do car manufactures release cars that are broken? No they release cars that are finished. No body will finish your script for you, its a custom function, it is in the package. This is not some big open source project that is well known, nor even a company project, so what would be the point in someone else finishing this?
    Car manufacutrers release cars that require some user configuration. this is exactly what the script is. every programmer sanitizes things differently.

    Nobody else needs to "finish it". They can simply delete the security function or implement their own. Or google how to secure/sanitize post/get values. it's not rocket science. also, i would edit the original post but it's not letting me because I think it's too old. So here is the config file:

    PHP Code:
    <?
    $db 
    = new mysqli("localhost""****","*****","****");

    if(
    $db->connect_errno 0){
        die(
    'Unable to connect to database [' $db->connect_error ']');
    }

        function 
    security($str)
        {
            return 
    addslashes(htmlentities(strip_tags(trim($str))));
        }


    ?>
    Last edited by markman641; 09-14-2013 at 03:55 AM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •