473,508 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to select image from folder, when image has random name and extension?

ilya Kraft
134 New Member
Hello,

I need to select image from a member folder to set it as their profile image. The problem is that I don't know how to do this, because that image has no fixed name or extension, so every time there is a different name for it. I need to use PHP for this, something like:

Expand|Select|Wrap|Line Numbers
  1. //This is not real code, just logic
  2. Select image from members/$member_id/image
  3.  
Here are scripts that I use to upload and store image in member folder.

Upload Form
Expand|Select|Wrap|Line Numbers
  1. <form enctype="multipart/form-data" method="post" action="image_upload_script.php">
  2.     Choose your file here:
  3.     <input name="uploaded_file" type="file"/><br /><br />
  4.     <input type="submit" value="Upload It"/>
  5.     </form>
  6.  
image_upload_script.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (!$_SESSION['idx']) { 
  3.     $msgToUser = '<br /><br /><font color="#FF0000">Only site members can do that</font><p><a href="register.php">Join Here</a></p>';
  4.     include_once 'msgToUser.php'; 
  5.     exit(); 
  6. } else if ($logOptions_id != $_SESSION['id']) {
  7.     $msgToUser = '<br /><br /><font color="#FF0000">Only site members can do that</font><p><a href="register.php">Join Here</a></p>';
  8.     include_once 'msgToUser.php'; 
  9.     exit(); 
  10. }
  11. $id = $logOptions_id;
  12. // Access the $_FILES global variable for this specific file being uploaded
  13. // and create local PHP variables from the $_FILES array of information
  14. $fileName = $_FILES["uploaded_file"]["name"]; // The file name
  15. $fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; // File in the PHP tmp folder
  16. $fileType = $_FILES["uploaded_file"]["type"]; // The type of file it is
  17. $fileSize = $_FILES["uploaded_file"]["size"]; // File size in bytes
  18. $fileErrorMsg = $_FILES["uploaded_file"]["error"]; // 0 for false... and 1 for true
  19. $kaboom = explode(".", $fileName); // Split file name into an array using the dot
  20. $fileExt = end($kaboom); // Now target the last array element to get the file extension
  21. // START PHP Image Upload Error Handling --------------------------------------------------
  22. if (!$fileTmpLoc) { // if file not chosen
  23.     echo "ERROR: Please browse for a file before clicking the upload button.";
  24.     exit();
  25. } else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes
  26.     echo "ERROR: Your file was larger than 5 Megabytes in size.";
  27.     unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
  28.     exit();
  29. } else if (!preg_match("/\.(gif|jpg|png)$/i", $fileName) ) {
  30.      // This condition is only if you wish to allow uploading of specific file types    
  31.      echo "ERROR: Your image was not .gif, .jpg, or .png.";
  32.      unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
  33.      exit();
  34. } else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
  35.     echo "ERROR: An error occured while processing the file. Try again.";
  36.     exit();
  37. }
  38. // END PHP Image Upload Error Handling ----------------------------------------------------
  39. // Place it into your "uploads" folder mow using the move_uploaded_file() function
  40. $moveResult = move_uploaded_file($fileTmpLoc, "members/$id/$fileName");
  41. // Check to make sure the move result is true before continuing
  42. if ($moveResult != true) {
  43.     echo "ERROR: File not uploaded. Try again.";
  44.     unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
  45.     exit();
  46. }
  47. unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
  48.  
Jun 24 '11 #1
11 3840
Rabbit
12,516 Recognized Expert Moderator MVP
When you upload the image, you should update the user record with the path of the image.
Jun 24 '11 #2
ilya Kraft
134 New Member
Hi Rabbit,

Do you mean that, I should create a field like "profile_image" which contains path to the image?

So say I have a field "profile_image" Field set to "VARCHAR" and with path to default image as Length/Values "members/default.jpg".

So then I would just use something like:
Expand|Select|Wrap|Line Numbers
  1. $new_image = members/$id/$filename
  2.  
  3. $profile_image_sql = mysql_query("INSERT INTO myMembers (profile_image) VALUES('$new_image')")
  4.         or die (mysql_error());
  5.  
This is what I think would be right, not sure though. Could you please tell me if this will work and also I think that this method will automatically replace old image with new when user uploads another file, am I right? If I am right, could you also point where exactly should I place this lines of code so it actually works.

Thank You
Jun 24 '11 #3
Markus
6,050 Recognized Expert Expert
You would use UPDATE rather than INSERT. But that's the general idea, yes.
Jun 24 '11 #4
ilya Kraft
134 New Member
Thnx Markus, one thing I'm concerned about. Is it alright if I define path to default image in Length/Values ? I mean will it change when I use UPDATE ? Also where should I place the code? I'm not sure where the good place is.

Thank You
Jun 24 '11 #5
Markus
6,050 Recognized Expert Expert
It will change when you run an UPDATE query, yes.

The UPDATE should be run when you're happy that the file has indeed been uploaded, otherwise you could end up with a path in your database to a non-existent file!

Also, you do not need to unlink the temporary file paths - they're cleaned up by the system.
Jun 24 '11 #6
ilya Kraft
134 New Member
Hi,

Alright I think I've got it ))) Just in case, Is it a good idea to put UPDATE after line 40 in image_upload_script.php ?

Thank You again
Jun 24 '11 #7
Markus
6,050 Recognized Expert Expert
Yes - but make sure you've checked the return value of $moveresult before.
Jun 24 '11 #8
ilya Kraft
134 New Member
Hi,

I tried it out, but came over a problem. When I create a field "profile_image" I set it as a "VARCHAR" and for Lenght/Values I enter path to deafult image e.g. members/0/image.jpg. But when I click Save an error saying: This is not a number. pops out. Is it because I use VARCHAR ?

UPDATE: Fixed it, forgot that I need to define path as a default and then enter character number in Length/Values
Jun 24 '11 #9
Markus
6,050 Recognized Expert Expert
No. When you tell it you want the VARCHAR datatype, you need to also give it a (maximum) size, in characters, that can be stored. So, a field of type VARCHAR(200) will be able to store up to 200 characters. See the documentation for more info: http://dev.mysql.com/doc/refman/5.0/en/char.html
Jun 24 '11 #10
ilya Kraft
134 New Member
Wooh, I have it working now ))) Thank You very much, Now I'm concerned (again :) is it secure agains SQL injections? I have same code as in my first post here + I added this lines at the very end of image_upload_script.php

new variables are from other code parts

Expand|Select|Wrap|Line Numbers
  1. $img_sql = mysql_query("UPDATE myMembers SET profile_img='$resized_file' WHERE id='$id' LIMIT 1");
  2.  
Is it secure enough?
Jun 24 '11 #11
ilya Kraft
134 New Member
Alright,

It works cool ))), but there is a problem. When user uploads new image the old one still remains in their folder. Is there a way to delete old one, so user data takes less space?
Jun 25 '11 #12

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

Similar topics

2
1799
by: Wlad | last post by:
Hi, Does anybody knows how to make the browser lauch an external image editor (i.e. Photoshop) when the user clicks on a link like this : <a href=...
1
3619
by: Novice | last post by:
I'm afraid I will incur the wraith of Mr. Powell on this one - but I did read his #1 FAQ and some others and I still can't figure this out. I created this little c# app. and I have a PictureBox...
3
2209
by: RobertH | last post by:
Hello all. I have been hacking away trying to get a SQL image (jpeg) to render in a control or table row Without using the Response.BinaryWrite.... I think i might be on the verge but need a...
6
2507
by: Suraj Joneja | last post by:
Hi All, I've an image control on my ASP.net page. This displays an image named 'Logo.jpg' in the location '~\Images'. Another application can change this image. It can select any image and...
1
4675
by: reidarT | last post by:
I have oppened a solution in Vb.net with an image folder. When I have opened vb.net and then adds an image to the image folder, it is not updated. I have to strt vb.net all over. Am I missing...
3
12355
by: djpaul | last post by:
Hello guys, I'm busy with an photogallery and i wanted to implent this script i wrote to select a folder and put there the photo's in. But i only get 1 folder in the drop-down menu and the other is...
2
3414
by: Kuldeep | last post by:
Hi all, I have a requirement, where the user has to select a folder and all the files in that folder have to be imported to my databse. To acheive this i need a control which enables the user to...
0
2103
by: pradheepayyanar | last post by:
hey i have an requirement which should select a folder in the web browser. i have an <input> element with <input type="file" name="file" accept="text/plain"> normally the type=file will...
10
1650
by: Keith G Hicks | last post by:
I'm hoping there's a simple way to do this. I need to show a dummy image in an asp image object if the file is missing. Here's my asp.net 2.0 markup: <asp:Image ID="imgGrad" runat="server"...
3
3270
by: Rishabh Indianic | last post by:
Hi, I am used VS 2005 with c# for developing window mobile application. When i create a cab file i add Primary Output and Content files from project. The content file contain image folder. When...
0
7225
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
7123
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
7326
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
7383
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7046
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...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1557
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 ...

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.