Image uploading code, help me modify it

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zarich
    New Member
    • May 2010
    • 5

    Image uploading code, help me modify it

    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.

    Code:
     <tr>
                    <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>
                    <td>
                      <input name="img_uploaded" type="file" />                </td>
                  </tr>
    Code:
      //This function separates the extension from the rest of the file name and returns it
      function findexts ($filename)
      {
      $filename = strtolower($filename) ;
      $exts = split("[/\\.]", $filename) ;
      $n = count($exts)-1;
      $exts = $exts[$n];
      return $exts;
      }
      
      //This applies the function to our file
      $ext = findexts ($_FILES['uploaded']['name']) ; 
      if(isset($_FILES['img_uploaded'])){
      $img_ext=findexts($_FILES['img_uploaded']['name']);
      if($img_ext=='png'|| $img_ext=='jpeg' || $img_ext=='jpg' || $img_ext=='gif'){
      $path2 = dirname( __FILE__ );
      $slash2 = '/'; 
      define( 'BASE_DIR2', $path2 . $slash2 );
      $img_unqi_dir=uniqid('vid_img');
      $dirPath2 = BASE_DIR2 . "img/".$img_unqi_dir;
      @mkdir( $dirPath2, '0755' );
      $img_name=uniqid($upload_time);
      $img_db="http://www.mydomain.com/anime/media/img/".$img_unqi_dir."/".$img_name.".".$img_ext;
      $img_id=$dirPath2."/".$img_name.".".$img_ext;
      }
      else{
    	  echo '<script type="text/javascript">alert(\'Lo sentimos solo puede subir imagenes en formato gif,png,jpeg,jpg\')</script>';
    	  $ok=0;
      }
      }
      else{
    	  $img_db='http://www.mydomain.com/include/anime/vid_default.jpg';
      }
      
      if($ext=='flv'||$ext=='f4v'||$ext=='mp4'){
      //Crea el id unico
      $ran = uniqid (rand (),true);
      //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.
      $ran2 = $ran.".";
    My folder tree is like this:
    publichtml>anim e>media>img

    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>anim e>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>anim e>img but the php file should remain at its location publichtml>anim e>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.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    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:
    Code:
    $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:
    Code:
    error_reporting(-1);
    ... and that should be roughly it.

    Let us know how you get on.

    Comment

    • Zarich
      New Member
      • May 2010
      • 5

      #3
      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:
      Code:
      //This applies the function to our file
        error_reporting(-1);
        $ext = findexts ($_FILES['uploaded']['name']) ; 
        if(isset($_FILES['img_uploaded'])){
        $img_ext=findexts($_FILES['img_uploaded']['name']);
        if($img_ext=='png'|| $img_ext=='jpeg' || $img_ext=='jpg' || $img_ext=='gif'){
        $path2 = realpath(dirname(__FILE__) . '/../');
        $slash2 = '/'; 
        define( 'BASE_DIR2', $path2 . $slash2 );
        $dirPath2 = BASE_DIR2 . "img/";
        $img_name=uniqid($upload_time);
        $img_db="http://www.mydomain.com/anime/img/"."/".$img_name.".".$img_ext;
        $img_id=$dirPath2."/".$img_name.".".$img_ext;
        }
        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.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Wonderful :)

        Glad to be of help!

        Be sure to stop by any time if you need some help.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          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.

          Comment

          Working...