supanoob Posted December 20, 2006 Share Posted December 20, 2006 ok so i have the following code:[code]<?phpif ($_REQUEST[completed] == 1) { $source = "/public_html/player_images"; move_uploaded_file($_FILES['filename']['tmp_name'], "../$source/".$_FILES['filename']['name']);// Following code to fix line ends if (! eregi ('(gif|jpg|png)$',$_FILES['filename']['name'])) { $fi = file("../$source/".$_FILES['filename']['name']); $fi2 = fopen("../$source/".$_FILES['filename']['name'],"w"); foreach ($fi as $lne) { $n = rtrim ($lne); fputs ($fi2,"$n\n"); } }//?>[/code]and i would like to modify it so it changes the image name to a unique number (a predifined one e.g. account ID) and the size of the image to 250x250 (if it is bigger if not it will stay the size it is). can someone help me modify the code to do so? Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/ Share on other sites More sharing options...
HuggieBear Posted December 20, 2006 Share Posted December 20, 2006 With regards to the unique name, that's simple, you have the code already. Instead of calling it the same name as the original file, just use the unique name.[code]<?phpmove_uploaded_file($_FILES['filename']['tmp_name'], "../$source/".$unique_id);?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-145045 Share on other sites More sharing options...
supanoob Posted December 20, 2006 Author Share Posted December 20, 2006 thanks :d any idea on the resizing part? also when i do what you say to change the image name it takes away the type of file from the endit comes up as[quote]/public_html/player_images/1[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-145048 Share on other sites More sharing options...
supanoob Posted December 20, 2006 Author Share Posted December 20, 2006 ok so i got it working on an empty page (apart from the error i stated in my other post) but i went to put it into my actual game and now i get these errors when uploading something[quote]Warning: move_uploaded_file(www.twottk.com/profiles/HPIM0420.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/twottk/public_html/account.php on line 41Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpwa45UE' to 'www.twottk.com/profiles/HPIM0420.jpg' in /home/twottk/public_html/account.php on line 41[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-145088 Share on other sites More sharing options...
supanoob Posted December 22, 2006 Author Share Posted December 22, 2006 can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-146358 Share on other sites More sharing options...
HuggieBear Posted December 22, 2006 Share Posted December 22, 2006 You can't use a URL as a parameter to move_uploaded_file(). You need to use a filesystem path.e.g. /home/domains/yourdomain.com/htdocs/image.jpgnot: http://www.yourdomain.com/image.jpgRegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-146361 Share on other sites More sharing options...
supanoob Posted December 22, 2006 Author Share Posted December 22, 2006 i do have it as a path not a url :S Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-146460 Share on other sites More sharing options...
HuggieBear Posted December 22, 2006 Share Posted December 22, 2006 OK, please post your latest code.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-146516 Share on other sites More sharing options...
supanoob Posted December 23, 2006 Author Share Posted December 23, 2006 [code]<?php if ($step == 'upload_picture') { if ($_REQUEST[completed] == 1) { $source = "/public_html/profiles"; move_uploaded_file($_FILES['filename']['tmp_name'], "../$source/".$_FILES['filename']['name']);// Following code to fix line ends if (! eregi ('(gif|jpg|png)$',$_FILES['filename']['name'])) { $fi = file("../$source/".$_FILES['filename']['name']); $fi2 = fopen("../$source/".$_FILES['filename']['name'],"w"); foreach ($fi as $lne) { $n = rtrim ($lne); fputs ($fi2,"$n\n"); } }//echo "Your file has been uploaded."; } else { echo "This page allows you to upload a file to the demo directory on ourserver.<br><br><form enctype=multipart/form-data method=post><input type=hidden name=MAX_FILE_SIZE value=150000><input type=hidden name=completed value=1>Choose file to send: <input type=file name=filename> and<input type=submit></form><br>"; } }?>[/code]thats it :D Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-146848 Share on other sites More sharing options...
HuggieBear Posted December 24, 2006 Share Posted December 24, 2006 The code seems fine, it must be an issue with the paths...Why not try setting the path at the beginning as the full path, and using that rather than using relative paths?[code]<?php$path = '/home/twottk/public_html/profiles/'; // or whatever the path isif ($step == 'upload_picture'){ if ($_REQUEST[completed] == 1){ move_uploaded_file($_FILES['filename']['tmp_name'], $path.$_FILES['filename']['name']); // Following code to fix line ends if (! eregi ('(gif|jpg|png)$',$_FILES['filename']['name'])) { $fi = file($path.$_FILES['filename']['name']); $fi2 = fopen($path.$_FILES['filename']['name'],"w"); foreach ($fi as $lne) { $n = rtrim ($lne); fputs ($fi2,"$n\n"); } } }}?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-147260 Share on other sites More sharing options...
supanoob Posted December 24, 2006 Author Share Posted December 24, 2006 that worked fine thanks :D any idea on how to insert the file name into the database? since changing the file name to the unique id gets rid of the file type if you know what i mean, it comes out as 1 instead od 1.jpg etc Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-147277 Share on other sites More sharing options...
HuggieBear Posted December 24, 2006 Share Posted December 24, 2006 Glad that worked, if you show me the code you've got now, including the part of the code that renames the file to the unique id then I'll be in a better position to help.Huggie Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-147302 Share on other sites More sharing options...
supanoob Posted December 24, 2006 Author Share Posted December 24, 2006 see what im hoping to do is make each image name change to$Char_id.$image_uploaded.gif/jpg/png/jpegwhich for example would change it to1.1.gif/jpg/png/jpegsince this will allow everyone to upload images and the image name wouldnt be the samebut i cant get that to work :P[code]<?php if ($step == 'upload_picture') { if ($_REQUEST[completed] == 1) { $source = "/home/twottk/public_html/profiles/"; move_uploaded_file($_FILES['filename']['tmp_name'], "$source".$char_id.$image_uploaded);// Following code to fix line ends if (! eregi ('(gif|jpg|png)$',$_FILES['filename']['name'])) { $fi = file("$source".$_FILES['filename']['name']); $fi2 = fopen("$source".$_FILES['filename']['name'],"w"); foreach ($fi as $lne) { $n = rtrim ($lne); fputs ($fi2,"$n\n"); } }//echo "Your file has been uploaded."; } else { echo "This page allows you to upload a file to the demo directory on ourserver.<br><br><form enctype=multipart/form-data method=post><input type=hidden name=MAX_FILE_SIZE value=150000><input type=hidden name=completed value=1>Choose file to send: <input type=file name=filename> and<input type=submit></form><br>"; } }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-147324 Share on other sites More sharing options...
supanoob Posted December 28, 2006 Author Share Posted December 28, 2006 bump Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-148857 Share on other sites More sharing options...
HuggieBear Posted January 2, 2007 Share Posted January 2, 2007 In your code, you're referencing [code=php:0]$image_uploaded[/code] but I can;t see that defined anywhere. Can you provide your full code used for renaming the file?RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-151439 Share on other sites More sharing options...
supanoob Posted January 2, 2007 Author Share Posted January 2, 2007 That is the full code :S like i said i saw a reference which is what this is pretty much word for word cept the parts that needed changing :S Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-151774 Share on other sites More sharing options...
JackW Posted January 3, 2007 Share Posted January 3, 2007 I spent several days (weeks) trying to do something similar. Finally got it to working. It renames and uploads a picture to the server, then adds information including picture name to my data base. It may be of some help to you. The code is in my last post at:http://www.phpfreaks.com/forums/index.php/topic,117897.0.html Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-152541 Share on other sites More sharing options...
supanoob Posted January 8, 2007 Author Share Posted January 8, 2007 ^^Thanks for the code but it doesnt seem to want to get past the "This is a valid file" section of the code.[code] <?php if ($step == 'upload_picture') {if ($action == 'submit'){if (($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "image/jpg")){echo 'That is valid picture file.'; } else { echo 'There is an error in your file! Your file must be a jpg or gif format and no larger that 80k'; exit; } //This function separates the extension from the rest of the file name and returns itfunction findexts ($filename){$filename = strtolower($filename) ;$exts = split("[/\\.]", $filename) ;$n = count($exts)-1;$exts = $exts[$n];return $exts;}//This applies the function to our file$ext = findexts ($_FILES['file']['name']) ;//This line assigns a time stapm to a variable. You could also use a random number here if you prefer.$ran = $char_id.$char_image ;//This takes the timestamp (or random number) you generated and adds a . on the end, so it is ready of the file extension to be appended.$ran2 = $ran.".";//This assigns the subdirectory you want to save into... make sure it exists!//find this path using phpinfo.php$target = "/home/twottk/public_html/profiles/";//This combines the directory, the random file name, and the extension$target = $target . $ran2.$ext;$image = $ran2.$ext;//Writes the photo to the serverif(move_uploaded_file($_FILES['file']['tmp_name'], $target)){$sql2="UPDATE accounts SET char_image=$char_image+1 WHERE char_id='$char_id'";if(mysql_query($sql2))echo "Image Uploaded Successfully.<br><br>The new iage name is $ran2.$ext<br><br>";}else{echo 'Sorry, there was a problem uploading your file.';}}echo "<form enctype=\"multipart/form-data\" name=\picture\" action=\"account.php?step=upload_picture&action=submit\" method=\"post\"><!-- MAX_FILE_SIZE must precede the file input field --> <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"82000\" /> <!-- Name of input element determines name in $_FILES array --> Send this file: <input name=\"file\" type=\"file\" /> <input type=\"submit\" value=\"Send File\" /></form>"; } ?>[/code]thats all the code, any help is much apreciated Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-155939 Share on other sites More sharing options...
supanoob Posted January 11, 2007 Author Share Posted January 11, 2007 anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-158448 Share on other sites More sharing options...
supanoob Posted January 11, 2007 Author Share Posted January 11, 2007 anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-158449 Share on other sites More sharing options...
supanoob Posted January 17, 2007 Author Share Posted January 17, 2007 bump ??? ??? Quote Link to comment https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-162769 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.