craigeves Posted March 5, 2010 Share Posted March 5, 2010 Hello PhpFreaks Although I appreciate that you guys are probably hand-written coders i wondered if you could help? I have the following php that will upload an image to the server and rename the jpeg to that of the session user (MM_User). When a file is uploaded and renamed I also need this to be inserted into the mysql database. Can anyone lend any help? <?php //define a maxim size for the uploaded images in Kb define ("MAX_SIZE","100"); //get Session Name $username = $_SESSION['MM_Username']; //This function reads the extension of the file. It is used to determine if the file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } //This variable is used as a flag. The value is initialized with 0 (meaning no error found) //and it will be changed to 1 if an errro occures. //If the error occures the file will not be uploaded. $errors=0; //checks if the form has been submitted if(isset($_POST['Submit'])) { //reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; //if it is not empty if ($image) { //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //if it is not a known extension, we will suppose it is an error and will not upload the file, //otherwise we will do more tests if (($extension != "jpg")) { //print error message echo '<p>Unknown extension!</p>'; $errors=1; } else { //get the size of the image in bytes //$_FILES['image']['tmp_name'] is the temporary filename of the file //in which the uploaded file was stored on the server $size=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($size > MAX_SIZE*1024) { echo '<p>You have exceeded the size limit!</p>'; $errors=1; } //we will give an unique name, for example the time in unix time format $image_name=$username.'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname="../sites/images/profiles/".$image_name; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<p>Copy unsuccessfull!</p>'; $errors=1; }}}} //If no errors registred, print the success message if(isset($_POST['Submit']) && !$errors) { echo "<p>File Uploaded Successfully! Try again!</p>"; } ?> <!--next comes the form, you must set the enctype to "multipart/frm-data" and use an input type "file" --> <form name="newad" method="POST" enctype="multipart/form-data" > <table> <tr> <td><input type="file" name="image"></td> </tr> <tr> <td><input name="Submit" type="submit" value="Upload image"></td> </tr> </table> <input type="hidden" name="userID2" value="<?php echo $row_getUsers['userID']; ?>" /> </form> Link to comment https://forums.phpfreaks.com/topic/194201-uploaded-image-name-to-mysql-database/ Share on other sites More sharing options...
craigeves Posted March 5, 2010 Author Share Posted March 5, 2010 I worked out how to do it. Thanks anyway! Link to comment https://forums.phpfreaks.com/topic/194201-uploaded-image-name-to-mysql-database/#findComment-1021755 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.