472,352 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

Image uploading code, help me modify it

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.   }
  10.  
  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="http://www.mydomain.com/anime/media/img/".$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='http://www.mydomain.com/include/anime/vid_default.jpg';
  33.   }
  34.  
  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:
publichtml>anime>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>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
4 1548
Markus
6,050 Expert 4TB
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
Zarich
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="http://www.mydomain.com/anime/img/"."/".$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
Markus
6,050 Expert 4TB
Wonderful :)

Glad to be of help!

Be sure to stop by any time if you need some help.
May 6 '10 #4
Markus
6,050 Expert 4TB
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

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

Similar topics

2
by: Tom | last post by:
Hi, I created a webform with asp.net and c#. I need to upload image to server and record the path to that image in DB. Any sample code for...
2
by: raj chahal | last post by:
Hi there I want to let users upload a image to their directory and then update my database so whenever someone searches the database an image is...
5
by: Deejam | last post by:
hi, i've this situations where user will upload images..my code works fine for uploading images.. but i need to create auto thumbnail picture...
2
by: pdesai007 | last post by:
hi all, i am trying to upload a image on to server with asp and AJAX, but it is not worked. so please give me information that upload an...
2
by: suganya | last post by:
Hi Using FileUpload control to upload the image I have given the coding as using System; using System.Data; using System.Configuration;...
2
kamill
by: kamill | last post by:
Dear All, I am uploading a image file and after resize the image i am saving it. My script is working fine with small size images but when i am...
4
pradeepjain
by: pradeepjain | last post by:
Hii guys, I have a small requirement which before starting i want to be clear of how to go above it. Since javascript will not work...
5
by: sarika | last post by:
Hi all I want to upload an image in my website using ajax and php i.e without refreshing the page i want to upload image . Plz help its very urgent
4
by: brettokumar | last post by:
hi i my form im using ifram tag if i click a button from my form my ifram from will be open and im uploading a image the image is uploaded and it...
1
by: chennaibala | last post by:
can any one send me mutiple image upload program and save the file name with extension in mysql table.we must cheak uploaded file type like bmp or...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.