We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12684
    • 118 Posts
    I am close to being able to allow members to upload more images. However the problem I am having is that the php is trying to rename the second image the same way it renamed the first, by basically giving the image the username of the member.

    How can I add to this line so that the image will be given the members name plus a character. Such as the number 2

    if (!move_uploaded_file($_FILES['photo2']['tmp_name'], $UserSecondImage))


    I need to be able to add to the tmp_name
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You would probably be better off using a timestamp to add to the username. It will make rather ugly filenames, but it will be impossible for a filename to be duplicated.

      
      if (!move_uploaded_file($_FILES['photo2']['tmp_name'], $path.$username.time().'.jpg'))

      or something like that.

      http://php.about.com/od/advancedphp/ss/rename_upload_3.htm
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 12684
        • 118 Posts
        Interesting enough, that bit of code is giving me a premission denied error.

        PHP error debug
        Error: move_uploaded_file(1216068239.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied
        Error type/ Nr.: Warning - 2
        File: /home/site/public_html/assets/snippets/webloginpe/webloginpe.class.php
        Line: 2800
        Line 2800 source: if (!move_uploaded_file($_FILES[’photo2’][’tmp_name’], $path.$username.time().’.jpg’))
          • 12684
          • 118 Posts
          Ok this thing is kicking my ass. I have tried to implement a few different set ups. I even got it to upload but because of the way Scotty set this thing up it serves errors like Hors D’Oeuvres.

          I have had a small amount of success using this...
          if (!move_uploaded_file($_FILES['photo']['tmp_name'], $userImage.time()))


          This above will allow me to upload an image and I can fine it in the /userimages/ folder.
          Using a .gif file named 8_4_110[1].gif the end result will be 8_4_110[1].gif1216141196

          So I thought I was close!

          However ModX gives me this error:
          PHP error debug
            Error: 	getimagesize(/home/502view/public_html/assets/snippets/webloginpe/userimages/8_4_110[1].gif) [function.getimagesize]: failed to open stream: No such file or directory	 
            Error type/ Nr.: 	Warning - 2	 
            File: 	/home/502view/public_html/assets/snippets/webloginpe/webloginpe.class.php	 
            Line: 	2697	 
            Line 2697 source: 	list($width, $height) = getimagesize($userImage); 


          So then I go to getimagesize line and add to ($userimages)- I put ($userImage.time())

          I then have to add $userImage.time() to the // Resample section Where php recognizes the extensions. It looks like below:

          Note the "<----------"

          		// Resample
          		$image_p = imagecreatetruecolor($new_width, $new_height);
          		
          		switch ($_FILES['photo']['type'])
          		{
          			case 'image/jpeg':
          			case 'image/jpg': // added support for .jpg to the "default" support for .jpeg, so WLPE doesn't give a filetype error
          			case 'image/pjpeg': // fix for IE6, which handles the .jpg filetype incorrectly
          				$image = imagecreatefromjpeg($userImage.time());                <----------
          				$ext = '.jpg';
          				break;
          				
          			case 'image/gif':
          				$image = imagecreatefromgif($userImage.time());                    <----------
          				imageSaveAlpha($image, true);
          				imagesavealpha($image_p, true);
          				$trans = imagecolorallocatealpha($image_p,255,255,255,127);
          				imagefill($image_p,0,0,$trans);
          				$ext = '.gif';
          				break;
          				
          			case 'image/png':
          				$image = imagecreatefrompng($userImage.time());                  <----------
          				imageSaveAlpha($image, true);
          				imagesavealpha($image_p, true);
          				$trans = imagecolorallocatealpha($image_p,255,255,255,127);
          				imagefill($image_p,0,0,$trans);
          				$ext = '.png';
          				break;
          				
          			default	:
          				return $this->FormatMessage($this->LanguageArray[30]);
          				break;
          		}


          And then I have to add to the below due to another Modx error:
          unlink($userImage.time());


          Doing the above has no erros, However it does not add the time to the file. It starts to act as if the extra time code is not even there. It just uploads with the username.

            • 12684
            • 118 Posts
            I know this is asking a lot, but I really need help on this one.
              • 32699 ☆ A M B ☆
              • 427 Posts
              W. Shawn Wilkerson Reply #6, 18 years, 1 month ago
              I have to implement something very similar on a project I am working on.

              I believe what Soshite was discussing concerning timestamp is the full unix time stamp. If you really want to make it more unique then through it through a MD5 hash.


              I am working on something like this, but I need the file names placed into a database so i can blow away the files after seven days for non re submission.

              Think of something like a classified ad with a seven day time period. If the client does not come back and re issue the ad, the article and pic get deleted.

                Get your copy of MODX Revolution Building the Web Your Way http://www.sanitypress.com/books/modx-revolution-building-the-web-your-way.html

                Check out my MODX || xPDO resources here: http://www.shawnwilkerson.com