anthonydamasco Posted February 14, 2007 Share Posted February 14, 2007 Well, What I'm trying to do is upload an image and rename the image. I want the name of the image to be the same as the "ID" so it will be easy to just have $ID.jpg and there is the image. Any help would be great! Quote Link to comment Share on other sites More sharing options...
s0c0 Posted February 14, 2007 Share Posted February 14, 2007 Want do you mean by ID? Where is this ID you speak of? Is it a primary key in a mysql table? Anyways a quick google yeilds plenty of plausible solutions. Here are just a few: http://www.phpeasystep.com/workshopview.php?id=18 http://www.phpfreaks.com/quickcode/Rename-Files/549.php or better yet try php.net and you will find the rename() function: http://us2.php.net/manual/en/function.rename.php Quote Link to comment Share on other sites More sharing options...
zq29 Posted February 14, 2007 Share Posted February 14, 2007 You can use the move_uploaded_file() function, like so: move_uploaded_file($_FILES['image']['tmp_name'],"images/$id.jpg") Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 14, 2007 Share Posted February 14, 2007 my fully working example. <?php session_start(); // if the id is set in a link use get or just leave it if the id //is in a session. $id=($_GET['id']); // directory path. $uploaddir = 'upload_new/'; // get the dir to send file to and the file name. $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); // get the current file information. $file=$uploadfile; //get the . and file exstention. $ext = substr($file, -4); //convert varable to the uplaoded directory the new id //and extention. $uploadfile=$uploaddir.$id.$ext; //rename the file to the new one. @rename($file,$uplaodfile); // if all the conditions are correct send the file to the directory. if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)){ // success echoed echo " <font color='red'>File is valid, and was successfully uploaded.</font>"; }else { //unsuccesfull echoed echo "<font color='red'>File was unsuccesful sorry</font>"; } // show the form. echo" <form enctype='multipart/form-data' action='new_test.php' method='POST'> <input type='hidden' name='MAX_FILE_SIZE' value='30000000000000000000000'> send this file <input name='userfile' type='file' > <input type='submit' name='submit' value='Send File'> </form>"; ?> Quote Link to comment 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.