clausowitz Posted December 5, 2011 Share Posted December 5, 2011 Hi All, I have a code to upload a file to the server. Somehow the files don't get uploaded, although the record is add to the database. I have no clue why it doesn't work. The settings of the folder where the docs goto is set to 777. <?php $uploaddir = "documents/"; $path = $uploaddir.$document; //This function reads the extension of the file. $allowedExtensions = array("doc", "pdf", "xls"); function isAllowedExtension($fileName) { global $allowedExtensions; return in_array(end(explode(".", $fileName)), $allowedExtensions); } if($document != ""){ //AS LONG AS A FILE WAS SELECTED... //compare the size with the maxim size we defined and print error if bigger $fsize=filesize($_FILES['document']['tmp_name']); if (($fsize/1024) > $poidsMax*1024) { $msgToUser = '<br /><br /><font color="#FF0000">You exceeded the filesize limit. Document did not get posted.</font>'; include_once 'msgToUser.php'; $errors=1; } if(isAllowedExtension($HTTP_POST_FILES['document']['tmp_name'])) { if(copy($HTTP_POST_FILES['document']['tmp_name'], $path)){ //IF IT HAS BEEN COPIED... //GET FILE NAME $theFileName = $HTTP_POST_FILES['document']['name']; //GET FILE SIZE $theFileSize = $HTTP_POST_FILES['document']['size']; if ($theFileSize>999999){ //IF GREATER THAN 999KB, DISPLAY AS MB $theDiv = $theFileSize / 1000000; $theFileSize = round($theDiv, 1)." MB"; //round($WhatToRound, $DecimalPlaces) } else { //OTHERWISE DISPLAY AS KB $theDiv = $theFileSize / 1000; $theFileSize = round($theDiv, 1)." KB"; //round($WhatToRound, $DecimalPlaces) } } else { $msgToUser = '<br /><br /><font color="#FF0000">Ooops something went wrong.</font><p><a href="../index.php">Go Back</a></p>'; include_once 'msgToUser.php'; $errors=1; } } } $name = strip_tags($name, ''); $message = strip_tags($message, ''); $sql = mysql_query("INSERT INTO bulletin_board (posting_date, member_id, title, subject, picture, document) VALUES(now(),'$logOptions_id', '$name','$message', '$image_name', '$theFileName')") or die (mysql_error()); Marco Quote Link to comment https://forums.phpfreaks.com/topic/252531-file-upload-problem/ Share on other sites More sharing options...
awjudd Posted December 5, 2011 Share Posted December 5, 2011 What is the value of $document on line 3? I think you need to revisit that part of the code. Please Note: $HTTP_POST_FILES has been deprecated, you should be using $_FILES instead of it. ~awjudd Quote Link to comment https://forums.phpfreaks.com/topic/252531-file-upload-problem/#findComment-1294763 Share on other sites More sharing options...
xyph Posted December 6, 2011 Share Posted December 6, 2011 Agree with the above. You should post code that works stand-alone. Take anything unrelated, and code you know works out. Ideally, we can copy and paste your code and execute it, getting only the errors you need help with. Quote Link to comment https://forums.phpfreaks.com/topic/252531-file-upload-problem/#findComment-1294765 Share on other sites More sharing options...
clausowitz Posted December 6, 2011 Author Share Posted December 6, 2011 Here is all my code: <?php $image_name = ''; $theFileName = ''; $name=$_POST['name']; $message=$_POST['message']; $image = $HTTP_POST_FILES['image']['name']; $document = $HTTP_POST_FILES['document']['name']; $document = str_replace("#", "No.", $document); $document = str_replace("$", "Dollar", $document); $document = str_replace("%", "Percent", $document); $document = str_replace("^", "", $document); $document = str_replace("&", "and", $document); $document = str_replace("*", "", $document); $document = str_replace("?", "", $document); $document = str_replace(" ", "_", $document); $uploaddir = "documents/"; $path = $uploaddir.$document; $logOptions_id = $_SESSION['id']; $poidsMax = ini_get('post_max_size'); // Name and Message required if (( $name == "") || ( $message == "")) { print "<p align=center>Please go back to complete all fields!<p>"; echo $name; } else { //define a maxim size for the uploaded images in Kb define ("MAX_SIZE","1000"); //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 function reads the extension of the file. $allowedExtensions = array("doc", "pdf", "xls"); function isAllowedExtension($fileName) { global $allowedExtensions; return in_array(end(explode(".", $fileName)), $allowedExtensions); } $errors=0; if($image<>"") { //reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; 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 //otherwise we will do more tests if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { //print error message $msgToUser = '<br /><br /><font color="#FF0000">You tried to upload a picture with an unknown extension.</font>'; include_once 'msgToUser.php'; $errors=1; } else { //get the size of the image in bytes $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) { $msgToUser = '<br /><br /><font color="#FF0000">You exceeded the filesize limit. Picture did not get posted.</font>'; include_once 'msgToUser.php'; $errors=1; } //we will give an unique name, for example the time in unix time format $image_name=time().'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname="pictures/".$image_name; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { $msgToUser = '<br /><br /><font color="#FF0000">Ooops something went wrong.</font><p><a href="../index.php">Go Back</a></p>'; include_once 'msgToUser.php'; $errors=1; } } } } //If no errors registred, print the success message if(( $image) && !$errors) { $msgToUser = '<br /><br /><font color="#FF0000">Message has been posted successfully.</font><p><a href="../index.php">Go Back</a></p>'; include_once 'msgToUser.php'; } if($document != ""){ //AS LONG AS A FILE WAS SELECTED... //compare the size with the maxim size we defined and print error if bigger $fsize=filesize($_FILES['document']['tmp_name']); if (($fsize/1024) > $poidsMax*1024) { $msgToUser = '<br /><br /><font color="#FF0000">You exceeded the filesize limit. Document did not get posted.</font>'; include_once 'msgToUser.php'; $errors=1; } if(isAllowedExtension($HTTP_POST_FILES['document']['tmp_name'])) { if(copy($HTTP_POST_FILES['document']['tmp_name'], $path)){ //IF IT HAS BEEN COPIED... //GET FILE NAME $theFileName = $HTTP_POST_FILES['document']['name']; //GET FILE SIZE $theFileSize = $HTTP_POST_FILES['document']['size']; if ($theFileSize>999999){ //IF GREATER THAN 999KB, DISPLAY AS MB $theDiv = $theFileSize / 1000000; $theFileSize = round($theDiv, 1)." MB"; //round($WhatToRound, $DecimalPlaces) } else { //OTHERWISE DISPLAY AS KB $theDiv = $theFileSize / 1000; $theFileSize = round($theDiv, 1)." KB"; //round($WhatToRound, $DecimalPlaces) } } else { $msgToUser = '<br /><br /><font color="#FF0000">Ooops something went wrong.</font><p><a href="../index.php">Go Back</a></p>'; include_once 'msgToUser.php'; $errors=1; } } } $name = strip_tags($name, ''); $message = strip_tags($message, ''); $sql = mysql_query("INSERT INTO bulletin_board (posting_date, member_id, title, subject, picture, document) VALUES(now(),'$logOptions_id', '$name','$message', '$image_name', '$theFileName')") or die (mysql_error()); } $msgToUser = '<br /><br /><font color="#FF0000">Message has been posted successfully.</font><p><a href="../index.php">Go Back</a></p>'; include_once 'msgToUser.php'; ?> I know my code is not like a good example of php coding but I am only a beginner. Regards Marco Quote Link to comment https://forums.phpfreaks.com/topic/252531-file-upload-problem/#findComment-1294929 Share on other sites More sharing options...
Pikachu2000 Posted December 6, 2011 Share Posted December 6, 2011 Is that code from a tutorial, or did you write it all? Quote Link to comment https://forums.phpfreaks.com/topic/252531-file-upload-problem/#findComment-1294940 Share on other sites More sharing options...
clausowitz Posted December 6, 2011 Author Share Posted December 6, 2011 What is the value of $document on line 3? I think you need to revisit that part of the code. That is the title of the document that is being uploaded. for example: mydoc.doc. Marco Quote Link to comment https://forums.phpfreaks.com/topic/252531-file-upload-problem/#findComment-1294941 Share on other sites More sharing options...
clausowitz Posted December 6, 2011 Author Share Posted December 6, 2011 Pikachu, that depends on how you look at it. But yes it is a composition of examples online which I put together. I know it looks terrible but it worked for me until I added the upload document part. Marco Quote Link to comment https://forums.phpfreaks.com/topic/252531-file-upload-problem/#findComment-1294942 Share on other sites More sharing options...
Pikachu2000 Posted December 6, 2011 Share Posted December 6, 2011 Well, the reason I ask is there are so many outdated tutorials on the internet, and it looks like you managed to find one of them. Unfortunately, if you don't already know what good code looks like, you can't usually tell if code you see in a tutorial is good or not. Quote Link to comment https://forums.phpfreaks.com/topic/252531-file-upload-problem/#findComment-1294945 Share on other sites More sharing options...
clausowitz Posted December 6, 2011 Author Share Posted December 6, 2011 You are right and of course that is exactly the trap for people like me. for instance I didn't know that I have to use $_FILES instead of $HTTP_POST_FILES. Marco Quote Link to comment https://forums.phpfreaks.com/topic/252531-file-upload-problem/#findComment-1294947 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.