473,856 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Image uploading code, help me modify it

5 New Member
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>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.
May 5 '10 #1
4 1658
Markus
6,050 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
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 Recognized Expert Expert
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
1366
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
1544
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 comonents . I am famailiar with asp somewhat. Does anyone know the best way to do this ? What are the advantages of using components ? my concern is that i may run ito problems when I update my database. Thanks
5
2055
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... help me how to create thumbnail picture.. is my question clear? thk in advance
2
1585
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 refeshing my original page. thank you,
2
1176
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 System.Web.UI;
2
2063
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 error. Error 500 - Internal server error An internal server error has occured! Please try again later. My ini settings are as below
4
10736
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 we can store the image when a user select an image in a temp on server and then give a preview to him. How to go on with this or is there any example for reference. thanks, pradeep
5
5445
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
1233
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 display in my current form in ifram tag how to i refersh only my ifram tage please shar ur ideas
1
4890
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 program,which can upload many file in folder.problem is,am unable to save my file name in to database.because i used same name for all input file type as file. i dont know get name of file.below i presented my coding for ur view.fed up with my coding..pls...
0
9906
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9758
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11051
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9528
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5956
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4171
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3196
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.