Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Junior Member MizzRedd's Avatar
    Join Date
    Aug 2012
    Posts
    2
    Member #
    32563
    Hello!

    I am working on a website that would allow the customers to customize their t-shirts by uploading their own image, place and resize it on the t-shirt. I have come across an issue though. After uploading the image into the 'uploads/' folder, it will not show up in the div for the customer to move and resize it.

    Here is the code that I am using:

    Code:
    <?php
    $dir = 'uploads/';
    $base_url = 'http://www.myurl.com/uploads';
    $newest_mtime = 0;
    $show_file = 'BROKEN';
    if ($handle = opendir($dir)) {
     while (false !== ($file = readdir($handle))) {
        if (($file != '.') && ($file != '..')) {
           $mtime = filemtime("$dir/$file");
           if ($mtime > $newest_mtime) {
              $newest_mtime = $mtime;
              $show_file = "$base_url/$file";
           }
        }
      }
    }
    print '<img src="' .$show_file. '" alt="Image Title Here">';
    ?>
    I guess I don't know how to tell the code in the div to show the file? Could anyone help? I am ready to rip my hair out.

    Thank you in advance

  2.  

  3. #2
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Sorry to say it, but I think you've gotta scrap your code. You're going to run into a race condition where two people upload photos in the same second, and both get the same photo to edit (one will be the wrong photo). And besides that, as your upload folder grows, that script is going to peg your server's processor. Bad idea to loop through a dir that could grow to hundreds of thousands of files.

    What you should do is use the upload script to get the final path / filename of the photo the user just uploaded and return the URL for it. If you're redirecting in the process, pass it as a querystring param.

    Sorry I don't have more help for you!

    FYI, if you were going to keep the logic the way you're doing it, the fastest way would be to grab the file name using:

    $file = shell_exec("ls -1tr {$dir}");

    Also FYI, I think your $dir name is what's wrong with your script. Try using an absolute path from root (e.g. /home/mizzred/public_html/uploads/)

  4. #3
    Junior Member MizzRedd's Avatar
    Join Date
    Aug 2012
    Posts
    2
    Member #
    32563
    Wow, I didn't even consider the fact that more than one person would be on the site at the same time. Thank you for pointing that out. What code would I use to to find the final filename? This is the code that I am using for my upload form:

    Code:
    define('DESTINATION_FOLDER','uploads/');
    define('MAX_FILE_SIZE', 0);
    $exts = array('jpg','jpeg','gif');
    define('RENAME_FILE', true);
    define('APPEND_STRING', '');
    
    ####################################################################
    ###  END OF SETTINGS.  DO NOT CHANGE BELOW
    ####################################################################
    
    // Allow script to work long enough to upload big files (in seconds, 2 days by default)
    @set_time_limit(172800);
    
    // following may need to be uncommented in case of problems
    // ini_set("session.gc_maxlifetime","10800");
    
    function showUploadForm($message='') {
      $max_file_size_tag = '';
      if (MAX_FILE_SIZE > 0) {
        // convert to bytes
        $max_file_size_tag = "<input name='MAX_FILE_SIZE' value='".(MAX_FILE_SIZE*1024)."' type='hidden' >\n";
      }
    
      // Load form template
      include ('uploadform.html');
    }
    
    // errors list
    $errors = array();
    
    $message = '';
    
    // we should not exceed php.ini max file size
    $ini_maxsize = ini_get('upload_max_filesize');
    if (!is_numeric($ini_maxsize)) {
      if (strpos($ini_maxsize, 'M') !== false)
        $ini_maxsize = intval($ini_maxsize)*1024*1024;
      elseif (strpos($ini_maxsize, 'K') !== false)
        $ini_maxsize = intval($ini_maxsize)*1024;
      elseif (strpos($ini_maxsize, 'G') !== false)
        $ini_maxsize = intval($ini_maxsize)*1024*1024*1024;
    }
    if ($ini_maxsize < MAX_FILE_SIZE*1024) {
      $errors[] = "Alert! Maximum upload file size in php.ini (upload_max_filesize) is less than script's MAX_FILE_SIZE";
    }
    
    // show upload form
    if (!isset($_POST['submit'])) {
      showUploadForm(join('',$errors));
    }
    
    // process file upload
    else {
    
      while(true) {
    
        // make sure destination folder exists
        if (!@file_exists(DESTINATION_FOLDER)) {
          $errors[] = "Destination folder does not exist or no permissions to see it.";
          break;
        }
    
        // get file name (not including path)
        $filename = @basename($_FILES['filename']['name']);
    
        // filename of temp uploaded file
        $tmp_filename = $_FILES['filename']['tmp_name'];
    
        $file_ext = @strtolower(@strrchr($filename,"."));
        if (@strpos($file_ext,'.') === false) { // no dot? strange
          $errors[] = "Suspicious file name or could not determine file extension.";
          break;
        }
        $file_ext = @substr($file_ext, 1); // remove dot
    
        // check file type if needed
        if (count($exts)) {  /// some day maybe check also $_FILES['user_file']['type']
          if (!@in_array($file_ext, $exts)) {
            $errors[] = "Files of this type are not allowed for upload.";
            break;
          }
        }
    
        // destination filename, rename if set to
        $dest_filename = $filename;
        if (RENAME_FILE) {
          $dest_filename = md5(uniqid(rand(), true)) . '.' . $file_ext;
        }
        // append predefined string for safety
        $dest_filename = $dest_filename . APPEND_STRING;
    
        $filesize = intval($_FILES["filename"]["size"]); // filesize($tmp_filename);
        if (MAX_FILE_SIZE > 0 && MAX_FILE_SIZE*1024 < $filesize) {
          $errors[] = "File is too big (3).";
          break;
        }
    
        if (!@move_uploaded_file($tmp_filename , DESTINATION_FOLDER . $dest_filename)) {
          $errors[] = "Could not upload file (6).";
          break;
        }
    
        // redirect to upload success url
        header('Location: ' . SUCCESS_URL);
        die();
    
        break;
    
      } // while(true)
    
      // Errors. Show upload form.
      $message = join('',$errors);
      showUploadForm($message);
    
    }
    Also, should the php for the upload form be in the same file as the php for the site that I am trying to display the image?

  5. #4
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    You could just change this one line:

    header('Location: ' . SUCCESS_URL);

    to this:

    header('Location: ' . SUCCESS_URL . '?filename=' . $dest_filename);


Remove Ads

Posting Permissions

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

Search tags for this page

displaying images from web folder using php
,
how to display latest image in a folder using php
,
php web page that shows latest image from a directory
,

upload and display image in php

,

upload image and display in php

Click on a term to search for related topics.
All times are GMT -6. The time now is 04:53 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com