By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,038 Members | 646 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,038 IT Pros & Developers. It's quick & easy.

Image uploading code, help me modify it

P: 5
This is a part of a code that i use to upload a video and a picture,but i want to modify the img part to upload the file in a diferent folder and i don't want it to create an unique folder for each picture.

Expand|Select|Wrap|Line Numbers
  1.  <tr>
  2.                 <td>Selecciona la imagen que representara el video :<div class="upload_sub">Esta sera la imagen que visualizaran los usuarios, debe ser un screenshot del video a subir.</div></td>
  3.                 <td>
  4.                   <input name="img_uploaded" type="file" />                </td>
  5.               </tr>
Expand|Select|Wrap|Line Numbers
  1.   //This function separates the extension from the rest of the file name and returns it
  2.   function findexts ($filename)
  3.   {
  4.   $filename = strtolower($filename) ;
  5.   $exts = split("[/\\.]", $filename) ;
  6.   $n = count($exts)-1;
  7.   $exts = $exts[$n];
  8.   return $exts;
  9.   }
  11.   //This applies the function to our file
  12.   $ext = findexts ($_FILES['uploaded']['name']) ; 
  13.   if(isset($_FILES['img_uploaded'])){
  14.   $img_ext=findexts($_FILES['img_uploaded']['name']);
  15.   if($img_ext=='png'|| $img_ext=='jpeg' || $img_ext=='jpg' || $img_ext=='gif'){
  16.   $path2 = dirname( __FILE__ );
  17.   $slash2 = '/'; 
  18.   define( 'BASE_DIR2', $path2 . $slash2 );
  19.   $img_unqi_dir=uniqid('vid_img');
  20.   $dirPath2 = BASE_DIR2 . "img/".$img_unqi_dir;
  21.   @mkdir( $dirPath2, '0755' );
  22.   $img_name=uniqid($upload_time);
  23.   $img_db="".$img_unqi_dir."/".$img_name.".".$img_ext;
  24.   $img_id=$dirPath2."/".$img_name.".".$img_ext;
  25.   }
  26.   else{
  27.       echo '<script type="text/javascript">alert(\'Lo sentimos solo puede subir imagenes en formato gif,png,jpeg,jpg\')</script>';
  28.       $ok=0;
  29.   }
  30.   }
  31.   else{
  32.       $img_db='';
  33.   }
  35.   if($ext=='flv'||$ext=='f4v'||$ext=='mp4'){
  36.   //Crea el id unico
  37.   $ran = uniqid (rand (),true);
  38.   //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
  39.   $ran2 = $ran.".";
My folder tree is like this:

This file is inside the media folder and it works perfectly, but i need to modify the script so it can upload picture to this address publichtml>anime>img , the only thing that i was able to do taking the "media/" part of the code was to create another folder inside anime/img but it didn't upload the picture and it also created a unique folder on its default location (anime/media/img), so im kinda stucked in here.

So i repeat just in order to make it clear, i want to take out the unique id folder creation (i removed the mkdir but i just got an error),and modify it to upload on publichtml>anime>img but the php file should remain at its location publichtml>anime>media.

This script was done by my partner but he has been out of reach, and im not good with php, so please help me out, i will appreciate that.
May 5 '10 #1
Share this Question
Share on Google+
4 Replies

Expert 5K+
P: 6,050
Hi there, Zarich.

The code you posted is pretty cryptic, so bear with me... it may take a couple of attempts to get this working.

The simplest way to do this with the current code would be to use realpath() to resolve a CDUP operator ('../') appended to the current directory. So, let's try that.

Make the following adjustment to line 16:
Expand|Select|Wrap|Line Numbers
  1. $path2 = realpath(dirname(__FILE__) . '/../');
$path2 should then point to the directory above the CWD (current-working-directory).

Note: if the video file upload *also* uses this variable/constant, it will upload the files to the wrong location. Unfortunately, with the current code-snippet, I cannot tell whether it does or doesn't.

Next, you can get rid of the mkdir() call on line 21.

Lastly, to aid in our attempts, stick this at the *very beginning* of your script:
Expand|Select|Wrap|Line Numbers
  1. error_reporting(-1);
... and that should be roughly it.

Let us know how you get on.
May 5 '10 #2

P: 5
Hey man,sorry for my late reply , and thanks for helping me out. I did what you told me to do, and i also deleted the $img_unqi variable and the "/img/" from the img_db variable. So here it is how it ended:
Expand|Select|Wrap|Line Numbers
  1. //This applies the function to our file
  2.   error_reporting(-1);
  3.   $ext = findexts ($_FILES['uploaded']['name']) ; 
  4.   if(isset($_FILES['img_uploaded'])){
  5.   $img_ext=findexts($_FILES['img_uploaded']['name']);
  6.   if($img_ext=='png'|| $img_ext=='jpeg' || $img_ext=='jpg' || $img_ext=='gif'){
  7.   $path2 = realpath(dirname(__FILE__) . '/../');
  8.   $slash2 = '/'; 
  9.   define( 'BASE_DIR2', $path2 . $slash2 );
  10.   $dirPath2 = BASE_DIR2 . "img/";
  11.   $img_name=uniqid($upload_time);
  12.   $img_db=""."/".$img_name.".".$img_ext;
  13.   $img_id=$dirPath2."/".$img_name.".".$img_ext;
  14.   }
  15.   else{
And guess what?, it worked perfectly on the first try, so Markus you are a genius, i really appreciate your help, i already love this web.

Btw about your concern, the video file upload uses another variable so it wasn't a problem at all.
May 6 '10 #3

Expert 5K+
P: 6,050
Wonderful :)

Glad to be of help!

Be sure to stop by any time if you need some help.
May 6 '10 #4

Expert 5K+
P: 6,050
Oh, and one extra note: only use error_reporting(-1); when you are debugging your PHP. If everything works and your site is live, remove that.
May 6 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.