473,661 Members | 2,501 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_sc ript.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 3866
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_im age" which contains path to the image?

So say I have a field "profile_im age" 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_sc ript.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_im age" 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

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

Similar topics

2
1808
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= "http://localhost/images/200411081456170.Nenuphars.jpg">Image</a> The same application can be launched for any image link. Thank you. Wlad
1
3633
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 in my Form. I load this image from the filesystem into the PictureBox and then I draw random little lines on the image. Then when I minimize and reopen the application the little lines are gone. Is there a way to save my lines in memory and...
3
2212
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 little help.. below are 2 functions that i wrote to try and accomplish this. the first one is what i would LIKE to do the 2nd one works but it's not what i want to do.. here's the code :
6
2514
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 upload to the folder 'Images' and newly selected image would overwrite existing image 'Logo.jpg'. (Image name remains same and image changes). The image control on my page doesn't refresh the newly selected image. it displays the old image where as the...
1
4706
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 something? reidarT
3
12409
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 standing next to it. If i do print_r ($folders); then the folders are side by side and the array has only index. Anybody an idea? this is my script: <? echo "<select name=\"selecteer folder\" size=\"1\" >"; $dir = ".";
2
3425
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 select only a "FOLDER", and does not expect the user to select a file. When "html - fileInput" or "asp:FileUpload" is used, these controls do not accept a selected folder and expects a single file to be selected.
0
2161
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 accept only one file @ a time. i need to select a folder. is that possible. if so how to do abt tat. if not wats the alternate option
10
1662
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" BorderColor="DimGray" BorderStyle="Solid" BorderWidth="1px" Height="120px" ImageUrl='<%# "~/Images/ClassmatePics/" & Eval("GradPhotoFileName") %>' ToolTip="Click to enlarge" /></td> "GradPhotoFileName" is stored in a field in a table. If the file...
3
3274
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 I install application in device using cab file it create a folder which contain the images that i have used in project By Programmatically by accessing path " (//Program FIles//Images//Image.jpg1) " it work well. problem is that this content file is...
0
8855
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
8758
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
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
7364
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
5653
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
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
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.