473,395 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1634
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 image uploading? Thanks for help Tom
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 displayed.. I've seen some scripts that use...
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 (of the same image) whenever user upload images......
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 image to the server with java script without...
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; using System.Web; using System.Web.Security; using...
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 trying to upload big size image, i am getting below...
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 for image preview i need to write a php code so that...
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 is diosplay in ifram form but the image can't...
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 any image file while uploading. i develop...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.