...

View Full Version : Using a customer's last name as an uploaded file name?



stephmck990
03-09-2009, 03:36 PM
I want to thank you all in advance for taking the time to look over my post and try to help me out here. :)

So, I'm fairly clueless about PHP, and the only thing I have to go on are the small things I'm finding here on the web. Although, I'm having a really tough time finding exactly what I'm looking for. I've found a few posts with the same questions as mine (http://codingforums.com/showthread.php?t=23626), but none that I can really understand... :rolleyes:

Okay. We have a simple form here with a text box and a file field.


<table width="100%" height="70" border="0">
<tr>
<td height="68">
<form name="onephotoupload" enctype="multipart/form-data" method="post" action="http://customcameracollection.net/customerphotos/upload_file.php">
<div align="center"> <font size="2" face="Verdana, Arial, Helvetica, sans-serif">

<table width="100%" border="0">
<tr>
<td colspan="2"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">
<center>
<label>Last Name:</label>
<input name="lastname" type="text" id="lastname" size="25">
<br /><input name="uploadedfile" type="file" id="uploadedfile" size="25">
</center>
</font></td>
</tr>
<tr>
<td width="58%" align="right">
<font size="2" face="Verdana, Arial, Helvetica, sans-serif">
<input type="submit" name="submit" value="Upload Photo" />
</font>
</td>
</tr>
</table>

<br />
</font></div>
</form></td>
</tr>
</table>

Now, I need a code that will take whatever the client types into the 'lastname' textbox and make it the new filename of the file that's uploaded to my FTP server. This way it would be easier for us to match up the client's photo with their order in which they placed. Each client will only be uploading one photo. No need for crazy numbers and letters and stuff.

This is the PHP I've got so far:


<?php

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>


If someone could let me know the simplest way of going about doing this, I'd be incredibly grateful.

Thanks again for taking the time to help me out! :thumbsup:

abduraooft
03-09-2009, 03:54 PM
Try

$target_path = "uploads/";

$target_path = $target_path . rawurlencode( $_POST['lastname']); You need to validate the 'lastname' for empty string.

PS:
Warning : If the destination file already exists, it will be overwritten.

shadowmaniac
03-09-2009, 04:04 PM
You may want to take measures in the event whereby two or more users' last name clash as they will potentially end up erasing each other's pictures.

stephmck990
03-09-2009, 04:19 PM
Awesome. Guys, thanks so much. I'll play around with how we want to end up doing this. (Might end up having the customer do last name, then first name separated by an underscore)

Huge help. :)

abduraooft
03-09-2009, 04:26 PM
(Might end up having the customer do last name, then first name separated by an underscore) ..until a situation comes where two customers have exactly the same first-name and last-name :)

stephmck990
03-09-2009, 04:38 PM
..until a situation comes where two customers have exactly the same first-name and last-name :)

Doh!!!

Hmm... any thoughts?

abduraooft
03-09-2009, 04:45 PM
No need for crazy numbers and letters and stuff. If you are ready to change the above decision, then it's easy.
You could append the auto_increment integer value from your table(assuming you are using such a DB setup) or just append a random number (see eg at http://php.net/manual/en/function.move-uploaded-file.php#85949)

stephmck990
03-09-2009, 05:55 PM
If you are ready to change the above decision, then it's easy.
You could append the auto_increment integer value from your table(assuming you are using such a DB setup) or just append a random number (see eg at http://php.net/manual/en/function.move-uploaded-file.php#85949)

Hmm. Actually I don't think we're keeping these images saved on the server for more than a day. Once they're uploaded, we're saving them into directories on our C drive. So I think we might be okay there.

So now since we're only using this form for photos, isn't there code to restrict the file field from accepting anything other than just images? (We might want to accept Word documents too...)

And also, size restrictions. We can't accept some dinky little cell phone photo for what our business does. Is there any way to keep client's from uploading a file under 50 kb?

Thanks again guys. You are too awesome for words. ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum