msa025 Posted February 26, 2008 Share Posted February 26, 2008 Hi everyone. I'm new to posting on these boards and relatively new to PHP. I am currently working on coding up a full site for my college radio station that I'm apart of. I'm trying to allow the djs to upload a picture of themselves and to allow the secretary to upload documents for download. All three functions were working just fine as of this morning, but now I keep getting error 7 when I use the move_uploaded_file. All of the permissions on the folder are set, and I didn't change the code from this morning to when the problem occurred, so I am unsure as to what is going on. The only other bit of information I have is the following: Array ( [upload] => Array ( [name] => t.jpg [type] => [tmp_name] => [error] => 7 => 0 ) ) and the php.ini settings areas follows: upload_max_filesize = 50M ;upload_tmp_dir= file_uploads=On The server is running FreeBSD. This is the university's MySQL server, so I am unable to change the php.ini file or any of the admin settings. I'm not sure if there is anything else I've left out...Anyways, here's the code as well. <?php if((!isset($_SESSION['first_name'])) || ($_SESSION['user_type'] < 3)) { $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); if((substr($url,-1) == '/') OR (substr($url,-1) == '\\')) { $url = substr($url,0,-1); } $url .= '/index.php'; ob_end_clean(); header("Location: $url"); exit(); } if (isset($_GET['id'])) { $id= $_GET['id']; } elseif (isset($_POST['id'])) { $id=$_POST['id']; } else { echo '<h1 id = mainhead">Page Error</h1><p class="error">This page has been accessed in error.</p><p><br /><br/></p>'; exit(); } if(isset($_POST['submitted'])) { require_once('mysql_connect.php'); $allowed = array('.jpg','.jpeg','.bmp','.gif'); $filename = $_FILES['upload']['name']; // Get the name of the file (including file extension). print_r($_FILES); $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename. if(in_array($ext,$allowed)) { if(isset($_FILES['upload'])) { $query = "SELECT fileName FROM djpics WHERE uploadID = '$id'"; $result = mysql_query($query); if(!$result) { $query = "INSERT INTO djpics(uploadID, fileName, fileSize, fileType) VALUES ('$id', '{$_FILES['upload']['name']}','{$_FILES['upload']['size']}','{$_FILES['upload']['type']}')"; } else { $query = "UPDATE djpics SET fileName = '{$_FILES['upload']['name']}', fileSize = '{$_FILES['upload']['size']}', fileType = '{$_FILES['upload']['type']}' WHERE uploadID = '$id'"; } $result = mysql_query($query); if($result) { if(move_uploaded_file($_FILES['upload']['tmp_name'], "djpics/" . $id)) { echo '<p>The file has been uploaded.</p>'; } else { $query = "DELETE FROM djpics WHERE uploadID = $id"; $result = mysql_query($query); echo '<p><font color="red">The file could not be uploaded because.</font></p>'; switch($_FILES['upload']['error']) { case 1: print 'The file exceeds the upload_max_filesize setting.'; break; case 2: print 'The file exceeds the MAX_FILE_SIZE setting.'; break; case 4: print 'The file was only partially uploade.'; break; case 6: print 'No temporary folder was available.'; break; default: print 'A system error occured.'; break; } } } else { echo '<p><font color="red">There was an error with the page.</font></p>'; } } else { echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>'; } } else { echo '<p><font color="red">The file must be either a .jpg, .jpeg, .gif, or .bmp!</font></p>'; } } ?> <form enctype="multipart/form-data" action="djpic.php" method="post"> <fieldset><legend>Upload a profile picture:</legend> <input type="hidden" name="MAX_FILE_SIZE" value="524288" /> <?php echo '<p><b>File:</b> <input type="file" name="upload" /></p><br /> </fieldset> <input type = "hidden" name="submitted" value="TRUE" /> <div align="center"><input type="submit" name="submit" value="Submit" /> <input type="hidden" name="id" value="' . $id . '"/></div> '; ?> </form> Anyways, please let me know if you have any ideas as to what I can do. I've been fighting with this all day, and I have completely run out of ideas on what could be wrong. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 26, 2008 Share Posted February 26, 2008 http://www.php.net/manual/en/features.file-upload.errors.php Quote Link to comment Share on other sites More sharing options...
msa025 Posted February 26, 2008 Author Share Posted February 26, 2008 Yea, I'm well aware of that page. The minute my code failed, I echoed the error number and searched php.net for it. None of the suggestions posted on that site have helped. Thank you for the reply however. Again, the problem seems to be intermittent. I'm just not overly sure why it would work for a while, and then not work for awhile... Quote Link to comment Share on other sites More sharing options...
aschk Posted February 26, 2008 Share Posted February 26, 2008 Your MAX_FILE_SIZE is probably wrong and you might find this corresponds with the error number (have you checked the docs for that error?) , 524288 looks too small. Quote Link to comment Share on other sites More sharing options...
msa025 Posted February 26, 2008 Author Share Posted February 26, 2008 I checked into that...but my files are smaller than the MAX_FILE_SIZE. When I'm testing these functions out, I typically use the same 2-3 files. Those files worked yesterday morning and when I coded them up on friday afternoon, but they didn't last night. I also tried to change the MAX_FILE_SIZE last night just in case, but it didn't change any of the errors, so I put it back as it was. So, the MAX_FILE_SIZE, while small, shouldn't be what's causing the issue. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 26, 2008 Share Posted February 26, 2008 Error 7 - "Can't write file" may be a permissions problem Quote Link to comment Share on other sites More sharing options...
msa025 Posted February 26, 2008 Author Share Posted February 26, 2008 checked that. Permissions are at 777 for all upload folders. Again, it works at some times, but then it won't at other times. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 26, 2008 Share Posted February 26, 2008 Insufficient disk space? Quote Link to comment Share on other sites More sharing options...
msa025 Posted February 26, 2008 Author Share Posted February 26, 2008 Checked that too...disk space is just fine. I can upload the file through dreamweaver or winscp with no problems, but I can't upload it via HTTP. I sent a copy of my code and my problem to my school's server admins (since it's not on my server), and I'm waiting to hear back from them in the event it's not something on their end. I was just hoping to have something to go with when I brought it up. Again, I'm pretty new at php and MySQL, so I wasn't sure if it was a problem with my code or what. And thanks for all the help so far. I really appreciate it. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 26, 2008 Share Posted February 26, 2008 Your code does seem back-to-front. I'd Check for error. if OK Move uploaded file if OK update the database whereas you do it the other way round. my 0.02. Quote Link to comment Share on other sites More sharing options...
msa025 Posted February 26, 2008 Author Share Posted February 26, 2008 I think I left it that way when I copied other code into this file. The other code needed to add the database entry first so it could name the file the same name as the uploadID. Is there any direct benefit to doing it the way you suggested as opposed to how I have implemented it? Or does it really not make much of a difference in the end? Quote Link to comment Share on other sites More sharing options...
Barand Posted February 26, 2008 Share Posted February 26, 2008 I think I left it that way when I copied other code into this file. The other code needed to add the database entry first so it could name the file the same name as the uploadID. OK, given that's the reason. I still think it maks sense not to go any further if there's an error though. Having said that, it won't make the error go away. 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.