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 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php script to upload jpg and create custom html file

    I've started looking for scripts some today and thought I'd post here to see if anyone could point me in a good direction..

    I need a php script that'll allow my co-worker to upload a jpg image to our webserver from a simple html form. The catch is, I also need the script to create a new html file in the same directory, with the same name as the jpg, and have links to the jpg file inside the html file.

    For example, my co-worker uploads a file called proof001.jpg to our server via html form, and with the help of php, a new html file is created in the same web directory called proof001.html, and within the code of the new html file, there is the line <img src="proof001.jpg"> among some more code that'll make up the webpage.

    I hope this is clear enough to understand, and hope that it very well is possible. So if anyone knows where this might currently exist, or like I said, could point me in the right direction of finding this code, please do so. Or, if this is simple enough to quickly right up, do please share. Thanks.

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It shouldn't be too hard to write something to do what you want. Here's some basic skeleton code that will handle the file upload, check it has the correct filename extension, create a html page with the title as the name of the file and the file itself displayed via the <img> tag.
    PHP Code:
    if(!isset($_POST) || empty($_POST)) {
        ?>
        <form name="upload_form" enctype="multipart/form-data" method="post" action="<?php echo basename($_SERVER['PHP_SELF']); ?>">
          <input type="file" name="userfile">
          <input type="submit" name="Submit" value="Submit">
        </form>
        <?php

    } else {

        
    // function to return filename and extension as an array
        
    function split_extension($filename){
            for(
    $i 1$i strlen($filename); $i++){
                if(
    substr($filename, -$i1) == '.'){
                    
    $filename_array['filename'] = substr($filename0, -$i);
                    
    $filename_array['extension'] = substr($filename, -$istrlen($filename));
                    return 
    $filename_array;
                }
            }
        }

        if(isset(
    $_FILES['userfile']) && !empty($_FILES['userfile'])) {
            
            
    // check the filename has the correct extension
            
    $allowed_extensions = array('.jpg''.gif''.png');
            
    $filename_array split_extension($_FILES['userfile']['name']);
            if(!
    in_array($filename_array['extension'], $allowed_extensions)) {
                echo 
    'You can only upload .gif, .jpg or .png files!';
                
    print_r($filename_array);
                exit();
            }

            
    // move the uploaded file to the target destination
            
    $uploaddir '/path/to/directory/';
            
    $uploadname $_FILES['userfile']['name'];
            if(!
    move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir $uploadname)) {
                echo 
    'The file could not be uploaded this time. <a href="' basename($_SERVER['PHP_SELF']) . '>Click here and try again.</a>';
                exit();
            }

            
    // create the html for the page
            
    $html '    <html>
                                <head>
                                <title>' 
    $filename_array['filename'] . '</title>
                                </head>
                                <body>
                                <img src="' 
    $_FILES['userfile']['name'] . '">
                                </body>
            '
    ;

            
    // create the html file and insert the html into it
            
    if(!$handle fopen($uploaddir $filename_array['filename'] . '.htm''w')) {
                echo 
    "Cannot open file.";
                exit;
            }
            if(
    fwrite($handle$html) === FALSE) {
                echo 
    "Cannot write to file.";
                exit;
            }
            echo 
    'Image uploaded and html file successfully created';
            
    fclose($handle);
        }
    }
    Obviously it'll need some expanding to handle what you want, there's no checking whether the upload returns an error or files exist with the selected names etc and the directory needs to be chmodded to 0777 for it to work, but it literally took 10 minutes so expanding upon it should be pretty simple.

  • #3
    New Coder
    Join Date
    Jun 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PERRRRRFECT!! This worked out great for me and I've got it all set up on my site exactly the way i wanted it. Thank you. I'll be sure to reply back if I've got any questions in the future about this but so far its flawless.


  •  

    Posting Permissions

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