mhallmon Posted June 12, 2011 Share Posted June 12, 2011 I am new to coding so please bare with me. I originally had my members edit_pic page to only upload one pic and overwrite the old pic if uploading a new one. I want my members to be able to upload multiple pics and have them saved to the membersFile/"Thier ID"/ directory on my server. After reading the tutorial from w3schools I decided to copy and paste their code, then change what needed to be changed. Here is the code: <?php session_start(); if (!isset($_SESSION['id'])) { echo 'Please <a href="login.php">log in</a> to access your account'; exit(); } $id = $_SESSION['id']; // Process the form if it is submitted if ($_FILES['uploadedfile']['tmp_name'] != "") { // Run error handling on the file // Set Max file size limit to 3mb $maxfilesize = 25165824; // Check file size, if too large exit and tell them why if($_FILES['uploadedfile']['size'] > $maxfilesize ) { echo "<br /><br />Your image was too large. Must be 3mb or less, please<br /><br /> <a href=\"edit_pic.php\">click here</a> to try again"; unlink($_FILES['uploadedfile']['tmp_name']); exit(); // Check file extension to see if it is .jpg or .gif, if not exit and tell them why } else if (!preg_match("/\.(gif|jpg)$/i", $_FILES['uploadedfile']['name'] ) ) { echo "<br /><br />Your image was not .gif or .jpg and it must be one of those two formats, please<br /> <a href=\"edit_pic.php\">click here</a> to try again"; unlink($_FILES['uploadedfile']['tmp_name']); exit(); // If no errors on the file process it and upload to server } else { // Rename the pic function 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['uploaded']['name']) ; $ran = rand () ; $ran2 = $ran."."; $target = "memberFiles/$id/"; $target = $target . $ran2.$ext; // Set the direntory for where to upload it, use the member id to hit their folder // Upload the file if (move_uploaded_file($_FILES['uploadedfile']['tmp_name']['tmp_name'], $target)) { echo "Success, the image has been uploaded and will display to visitors!<br /><br /> <a href=\"member_account.php\">Click here</a> to return to your profile edit area"; exit(); } else { echo "There was an error uploading the file, please try again. If it continues to happen please contact support. <br /><br /> <a href=\"member_account.php\">Click here</a> to return to your profile edit area"; exit(); } } // close else after file error checks } // close if post the form ?> Now, when I test this page it will always echo the error. Can anyone please tell me what I did wrong and help me fix it? Quote Link to comment https://forums.phpfreaks.com/topic/239163-uploading-multiple-images-assigning-a-random-number-to-file/ Share on other sites More sharing options...
fugix Posted June 12, 2011 Share Posted June 12, 2011 Which error are you constantly receiving? As on your code you have multiple error triggers. Also as a side note, be careful when using w3schools, here is why Quote Link to comment https://forums.phpfreaks.com/topic/239163-uploading-multiple-images-assigning-a-random-number-to-file/#findComment-1228787 Share on other sites More sharing options...
mhallmon Posted June 12, 2011 Author Share Posted June 12, 2011 Thanks for the fast reply and also, thanks for the info on W3school... After searching the net for hours... they were the only source I found that had information I was looking for without downloading software to do it for me. I would rather know how to do it. The error message I continue to get is : "There was an error uploading the file, please try again. If it continues to happen please contact support." Not sure why. Quote Link to comment https://forums.phpfreaks.com/topic/239163-uploading-multiple-images-assigning-a-random-number-to-file/#findComment-1228789 Share on other sites More sharing options...
mhallmon Posted June 12, 2011 Author Share Posted June 12, 2011 I found a better tutorial and tried the script... it worked. I will post it in a min just in-case anyone else ever has this issue Quote Link to comment https://forums.phpfreaks.com/topic/239163-uploading-multiple-images-assigning-a-random-number-to-file/#findComment-1228797 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.