PHP - [php] Upload File Script Problem
where is the problem?
http://pastebin.com/Smw92nd5 I'm looking for the folder where the uploaded file should be but it doesn't exist, it seems that the condition "if($ext == $need)" is ignored Similar TutorialsHello, all: been trying to convert this little single-file upload to multiple by naming each file form-field as "userfile[]" as it's supposed to automatically treat them as an array.. but no luck! Can you guide me as to what am I doing wrong?? appreciate the help! Code: [Select] <?php if (!isset($_REQUEST["seenform"])) { ?> <form enctype="multipart/form-data" action="#" method="post"> Upload file: <input name="userfile[]" type="file" id="userfile[]"> Upload file: <input name="userfile[]" type="file" id="userfile[]"> <input type="submit" value="Upload"> <input type="hidden" name="seenform"> </form> <?php } else { // upload begins $userfiles = array($_FILES['userfile']); foreach ($userfiles as $userfile) { // foreach begins $uploaded_dir = "uploads/"; $userfile = $_FILES['userfile']["name"]; $path = $uploaded_dir . $userfile; if (move_uploaded_file($_FILES['userfile']["tmp_name"], $path)) { print "$userfile file moved"; // do something with the file here } else { print "Move failed"; } } // foreach ends } // upload ends ?> Hey all So have been working on a file upload script, it was uploading the file but also adding the path name instead of NULL to mysql when no image was to upload, that is now fixed however now it won't upload the actual image to the directory. I tried undoing the mysql changes but it still won't upload the image to the directory. Am testing on my own computer using xampp so no file permission issues, plus it was working before. Any help much appreciated. Thanks <?php $product_code = mysqli_real_escape_string($conn, $_POST['product_code']); $product_name = mysqli_real_escape_string($conn, $_POST['product_name']); $category = mysqli_real_escape_string($conn, $_POST['category']); $filter = mysqli_real_escape_string($conn, $_POST['filter']); $description = mysqli_real_escape_string($conn, $_POST['description']); $specification = mysqli_real_escape_string($conn, $_POST['specification']); $price = mysqli_real_escape_string($conn, $_POST['price']); $target_dir = "../images/products/"; if (!isset ($_FILES["img1"]["name"])) { $target_file1 = NULL; } else { if (!empty($_FILES["img1"]["name"])) { $target_file1 = $target_dir . basename($_FILES["img1"]["name"]); } else { $target_file1 = NULL; } } if (!isset ($_FILES["img2"]["name"])) { $target_file2 = NULL; } else { if (!empty($_FILES["img2"]["name"])) { $target_file2 = $target_dir . basename($_FILES["img2"]["name"]); } else { $target_file2 = NULL; } } if (!isset ($_FILES["img3"]["name"])) { $target_file3 = NULL; } else { if (!empty($_FILES["img3"]["name"])) { $target_file3 = $target_dir . basename($_FILES["img3"]["name"]); } else { $target_file3 = NULL; } } if (!isset ($_FILES["img4"]["name"])) { $target_file4 = NULL; } else { if (!empty($_FILES["img4"]["name"])) { $target_file4 = $target_dir . basename($_FILES["img4"]["name"]); } else { $target_file4 = NULL; } } $uploadOk = 1; $imageFileType1 = strtolower(pathinfo($target_file1,PATHINFO_EXTENSION)); $imageFileType2= strtolower(pathinfo($target_file2,PATHINFO_EXTENSION)); $imageFileType3 = strtolower(pathinfo($target_file3,PATHINFO_EXTENSION)); $imageFileType4 = strtolower(pathinfo($target_file4,PATHINFO_EXTENSION)); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check1 = getimagesize($_FILES["img1"]["tmp_name"]); $check2 = getimagesize($_FILES["img2"]["tmp_name"]); $check3 = getimagesize($_FILES["img3"]["tmp_name"]); $check4 = getimagesize($_FILES["img4"]["tmp_name"]); if($check1 !== false) { echo "File is an image - " . $check1["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } if (file_exists($target_file1)) { echo "Sorry, image one already exists."; $uploadOk = 0; } if($imageFileType1 != "jpg" && $imageFileType1 != "png" && $imageFileType1 != "jpeg" && $imageFileType1 != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed for img1."; $uploadOk = 0; } if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["img1"]["tmp_name"], $target_file1)) { echo "The file ". htmlspecialchars( basename( $_FILES["img1"]["name"])). " has been uploaded."; } else { echo "Sorry, there was an error uploading image one."; } } echo '<br />'; if($check2 !== false) { echo "File is an image - " . $check2["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } if (file_exists($target_file2)) { echo "Sorry, image two already exists."; $uploadOk = 0; } if($imageFileType2 != "jpg" && $imageFileType2 != "png" && $imageFileType2 != "jpeg" && $imageFileType2 != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed for img2."; $uploadOk = 0; } if (isset ($target_file2)) { if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["img2"]["tmp_name"], $target_file2)) { echo "The file ". htmlspecialchars( basename( $_FILES["img1"]["name"])). " has been uploaded."; } else { echo "Sorry, there was an error uploading image two."; } } } echo '<br />'; if($check3 !== false) { echo "File is an image - " . $check3["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } if (file_exists($target_file3)) { echo "Sorry, image three already exists."; $uploadOk = 0; } if($imageFileType3 != "jpg" && $imageFileType3 != "png" && $imageFileType3 != "jpeg" && $imageFileType3 != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed for img3."; $uploadOk = 0; } if (isset ($target_file3)) { if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["img3"]["tmp_name"], $target_file3)) { echo "The file ". htmlspecialchars( basename( $_FILES["img3"]["name"])). " has been uploaded."; } else { echo "Sorry, there was an error uploading image three."; } } } echo '<br />'; if($check4 !== false) { echo "File is an image - " . $check4["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } if (file_exists($target_file4)) { echo "Sorry, image four already exists."; $uploadOk = 0; } if($imageFileType4 != "jpg" && $imageFileType4 != "png" && $imageFileType4 != "jpeg" && $imageFileType4 != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed for img4."; $uploadOk = 0; } if (isset ($target_file4)) { if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["img4"]["tmp_name"], $target_file4)) { echo "The file ". htmlspecialchars( basename( $_FILES["img4"]["name"])). " has been uploaded."; } else { echo "Sorry, there was an error uploading image four."; } } } } echo '<br />'; $image1 = basename($target_file1); $image2 = basename($target_file2); $image3 = basename($target_file3); $image4 = basename($target_file4); // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "INSERT INTO products (product_code, product_name, category, filter, description, specification, img1, img2, img3, img4, price) VALUES('$product_code', '$product_name', '$category', '$filter', '$description', '$specification', '$image1', '$image2', '$image3', '$image4', '$price')"; if (mysqli_query($conn, $sql)) { echo "Product Added successfully, Now on to the Sizes"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } ?>
Hello, I'm planning on making a website where people are able to upload files and directly link to them. Ideally, I want to keep the same file names that the people use when they upload the file. I was planning on keeping the directory that stored all of the files outside of the www directory and disable execute permissions. However, how would I avoid file overwriting with the same file name? Hi, I have exactly the same script and same php.ini file like this website : https://translatesubtitles.com and as you can see the proccess of that php website is: upload srt file redirection to translate.php file where the uploaded text is shown in the table my website script is : https://translatesubtitles.co and when I try to upload srt file it redirected me to the translate.php page but there is no text shown in the table, so what can be the problem? Hi: Is it possible to use the ereg() function in a file upload script so that the ereg() can recognize whether or not the file type is a .jpg, .gif, .png? I already figured out how to do it without using the ereg() function - but I'm just interested to know if it is possible using ereg()..if so, could you provide me with an example? I'm also a bit confused as to how to use ereg() to recognize file types. Here;s what I've done: Code: [Select] function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } if ($image) { $filename = stripslashes($_FILES['image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo '<h1>Unknown extension!</h1>'; $errors=1; } Thanks! Good Evening, If anyone could help with this little problem be much appreciated. what i'm trying to do is using a drop down menu select a folder name which is stored in a database table.. then taking that folder name and upload various files to that specified folder. There's probably an easier way to do this but i've searched google argggh.. The first part of code is the php to grab the folder names and the file selection form Code: [Select] <?php include("connection.php"); $sql="SELECT id, name FROM gallery"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $id=$row["id"]; $thing=$row["name"]; $options.="<OPTION VALUE=\"$id\">".$thing ; } ?> <table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form action="postFiles.php" method="post" enctype="multipart/form-data" name="form1" id="form1"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td><strong>multiple Files Upload </strong></td> </tr> <td><SELECT NAME=thing> <OPTION VALUE=0>Choose <?=$options?> </SELECT> </td> <tr> <td>Select file <input name="ufile[]" type="file" id="ufile[]" size="50" /></td> </tr> <tr> <td>Select file <input name="ufile[]" type="file" id="ufile[]" size="50" /></td> </tr> <tr> <td>Select file <input name="ufile[]" type="file" id="ufile[]" size="50" /></td> </tr> <tr> <td align="center"><input type="submit" name="Submit" value="Upload" /></td> </tr> </table> </td> </form> </tr> </table> The second part of the code below is the php code to upload those files, the problem being how do i specify the upload destination using the drop down in the previous form (instead of UPLOAD) which it is default set to Code: [Select] <?php //set where you want to store files //in this example we keep file in folder upload //$HTTP_POST_FILES['ufile']['name']; = upload file name //for example upload file name cartoon.gif . $path will be upload/cartoon.gif $path1= "upload".$HTTP_POST_FILES['ufile']['name'][0]; $path2= "upload".$HTTP_POST_FILES['ufile']['name'][1]; $path3= "upload".$HTTP_POST_FILES['ufile']['name'][2]; //copy file to where you want to store file copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1); copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2); copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3); //$HTTP_POST_FILES['ufile']['name'] = file name //$HTTP_POST_FILES['ufile']['size'] = file size //$HTTP_POST_FILES['ufile']['type'] = type of file echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>"; echo "File Size :".$HTTP_POST_FILES['ufile']['size'][0]."<BR/>"; echo "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>"; echo "<img src=\"$path1\" width=\"150\" height=\"150\">"; echo "<P>"; echo "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>"; echo "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>"; echo "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>"; echo "<img src=\"$path2\" width=\"150\" height=\"150\">"; echo "<P>"; echo "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>"; echo "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>"; echo "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>"; echo "<img src=\"$path3\" width=\"150\" height=\"150\">"; /////////////////////////////////////////////////////// // Use this code to display the error or success. $filesize1=$HTTP_POST_FILES['ufile']['size'][0]; $filesize2=$HTTP_POST_FILES['ufile']['size'][1]; $filesize3=$HTTP_POST_FILES['ufile']['size'][2]; if($filesize1 && $filesize2 && $filesize3 != 0) { echo "We have recieved your files"; } else { echo "ERROR....."; } ////////////////////////////////////////////// // What files that have a problem? (if found) if($filesize1==0) { echo "There're something error in your first file"; echo "<BR />"; } if($filesize2==0) { echo "There're something error in your second file"; echo "<BR />"; } if($filesize3==0) { echo "There're something error in your third file"; echo "<BR />"; } ?> Thanks in advance for any help if you need to see a live version of these files i can provide a link Hello everyone. I'm posting because I'm having difficulty using a file upload script I wrote in PHP. (Based on a tutorial naturally.) May someone more skilled than I could take a look at it? Form: (simple, no ajax or jquery involved. I'll add that later.) Code: [Select] <form enctype="multipart/form-data" action="upload.php" method="POST"> <table><tr><td>File</td><td><input name="uploaded" type="file" /></td></tr> <tr><td colspan="2"><input type="submit" value="Upload" style="float:right;" /></td></tr></table> </form> Upload Script: Code: [Select] <? session_start(); include('include/session.php'); /*IMAGE*/ $target = "upload/"; $target = $target . basename($_FILES['uploaded']['name']); $name = basename( $_FILES['uploaded']['name']); $ok=1; global $database; //This is our size condition if ($uploaded_size > 350000000) { echo "Your file is too large.<br>"; $ok=0; } //This is our limit file type condition if ($uploaded_type =="text/php") { echo "No PHP files<br>"; $ok=0; } //Here we check that $ok was not set to 0 by an error if ($ok==0) { Echo "Sorry your file was not uploaded"; } //If everything is ok we try to upload it else { if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { $channel_number = $_SESSION['channel_number']; $n = "INSERT INTO `documents` (`name`,`channel_number`) VALUES ('$name','$channel_number')"; mysql_query($n); header("Location:index.php"); } else { echo "Sorry, there was a problem uploading your file."; } } ?> The mysql query works fine, and it's just so that I can keep track of the uploads. I'm well aware this is not a secure script, btw. I'm just doing it to learn. Hi, I'm a PHP beginner using Julie Meloni Sams Teach yourself to help me. My confusion is with this script <?php $file_dir ="/path/to/upload/directory" ; foreach($_FILES as $file_name => $file_array) { echo "path: ".$file_array["tmp_name"]."<br/>\n" ; echo "name: ".$file_array["name"]."<br/>\n" ; echo "type: ".$file_array["type"]."<br/>\n" ; echo "File name is" . " ". $file_name ; } ?> What I don't understand is the foreach statement foreach($_FILES as $file_name => $file_array) $_FILES is an associative array, right? So $filename should represent the key id and $file_array the value. but she is using the $file_array as if it is an array and accessing the values with associative index which confuses. I mean, let's say I declared a normal array. $array = array("Sarah" => "Jane", "John Smith") ; if i wanted to use a foreach loop i would do it like so foreach($array as $fname => $surname) { echo $fname ; echo $surname ; } $fname will print the first name and surname surname. I didn't use an indices to access the values. foreach($_FILES as $file_name => $file_array) can someone explain this to me? Thanks Hi all, This is my very first post here, so I will try and make it count Very recently I have been working with php and I found this simple little script that allows users to upload files. I tweaked some things to only make it upload .doc files but now I have a slight problem. Every time someone uploads a word file with the same name, it gets replaced. I tried looking for other examples here on the forum, but was unable to apply it to my own script. It would be a big help if someone could provide a simple add-on to this existing script. Thanks in advance. Script: <? $locatie="upload/"; $toegestaan = "doc"; $max_size = 1000000; set_time_limit(0); if(isset($_POST['upload'])) { if(is_uploaded_file($_FILES['bestand']['tmp_name'])) { $extensie_bestand = pathinfo($_FILES['bestand']['name']); $extensie_bestand = $extensie_bestand[extension]; $extensies_toegestaan = explode(", ", $toegestaan); for($i = 0; $i < count($extensies_toegestaan); $i++) { if($extensies_toegestaan[$i] == "$extensie_bestand") { $ok = 1; } } if($ok == 1) { if($_FILES['bestand']['size']>$max_size) { echo "Het bestand is te groot, de maximale grootte is: <b>$max_size</b>"; exit; } if(!move_uploaded_file($_FILES['bestand']['tmp_name'], $locatie.$_FILES['bestand']['name'])) { echo "het bestand kan niet worden verplaatst"; exit; } echo "Het bestand ".$_FILES['bestand']['name']." is geupload<br> <a href='".$locatie."".$_FILES['bestand']['name']."' </a>"; } else { echo "Verkeerde extentie, de toegestane extensies zijn: <b>$toegestaan</b>"; } } else { echo "Het uploaden is mislukt, probeer het opnieuw"; } } ?> <title>test tittle</title> <style type="text/css"> <!-- body { margin-top: 0px; } --> </style></head> <body> <table width="1216" height="1191" border="0" cellpadding="0" cellspacing="0" background="back"> <tr> <td height="317" colspan="2"> </td> </tr> <tr> <td height="381"> </td> <td><p> </p> <form method="post" action="<?=$_SERVER['PHP_SELF']?>" enctype="multipart/form-data"> <table width="398" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="180"> </td> <td width="218"><input type="file" name="bestand" /> <input type="submit" name="upload" value="uploaden" /></td> </tr> <tr> <td> </td> <td> </td> </tr> </table> </form></td> </tr> <tr> <td width="420"> </td> <td width="796"> </td> </tr> </table> I know it needs a for loop, but i don't know where in the code i should be putting it? Code: [Select] function check_input($value) { // Stripslashes if (get_magic_quotes_gpc()) { $value = stripslashes($value); } // Quote if not a number if (!is_numeric($value)) { $value = "'" . mysql_real_escape_string($value) . "'"; } return $value; } $_POST = array_map('check_input', $_POST); $sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images) VALUES ({$_POST['customername']}, {$_POST['town']}, {$_POST['testimonial']}, {$_POST['sort_order']}, '$imgname' )"; } if (!mysql_query($sql,$con)) { die("<br>Query: $sql<br>Error: " . mysql_error() . '<br>'); } echo "<p align=center><b>1 testimonial added</b></p>"; mysql_close($con); Thanks in Advance, Steve I want to let users select and upload a file. The select form and upload sritp work when they are on different pages, but I want them on the same page with the upload script executing only if the form has been submitted. Here the upload form <!--select the file --> <form enctype="multipart/form-data" action="manage_files.php" method="POST">Please choose a file to upload: <input name="uploaded" type="file" /><input type="submit" value="Upload" /></form> ... and here's the upload code... <!--upload the file --> <?php if (isset($_POST['submit'])) { $target = "safes/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; //This is our size condition //if ($uploaded_size > 350000) //{ //echo "Your file is too large.<br>"; //$ok=0; //} //if (!($uploaded_type=="application/zip")) { //echo "You may only upload ZIP files.<br>"; //$ok=0; //} //Here we check that $ok was not set to 0 by an error if ($ok==0) { Echo "Sorry your file was not uploaded"; } //If everything is ok we try to upload it else { if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else { echo "Sorry, there was a problem uploading your file."; } } } ?> What am I missing? Thanks for any help! files that upload during insert/submit form was gone , only files upload during the update remain , is the way query for update multiple files is wrong ? $targetDir1= "folder/pda-semakan/ic/"; if(isset($_FILES['ic'])){ $fileName1 = $_FILES['ic']['name']; $targetFilePath1 = $targetDir1 . $fileName1; //$main_tmp2 = $_FILES['ic']['tmp_name']; $move2 =move_uploaded_file($_FILES["ic"]["tmp_name"], $targetFilePath1); } $targetDir2= "folder/pda-semakan/sijil_lahir/"; if(isset($_FILES['sijilkelahiran'])){ $fileName2 = $_FILES['sijilkelahiran']['name']; $targetFilePath2 = $targetDir2 . $fileName2; $move3 =move_uploaded_file($_FILES["sijilkelahiran"]["tmp_name"], $targetFilePath2); } $targetDir3= "folder/pda-semakan/sijil_spm/"; if(isset($_FILES['sijilspm'])){ $fileName3 = $_FILES['sijilspm']['name']; $targetFilePath3 = $targetDir3 . $fileName3; $move4 =move_uploaded_file($_FILES["sijilspm"]["tmp_name"], $targetFilePath3); } $query1=("UPDATE semakan_dokumen set student_id='$noMatrik', email= '$stdEmail', surat_tawaran='$fileName', ic='$fileName1',sijil_lahir='$fileName2',sijil_spm= '$fileName3' where email= '$stdEmail'");
Hello,
I have the following upload script that actually worked until I tried to add file type specifications. I can't figure out what I did wrong. I have been piecing things together so there is no telling what is wrong. Help me please?
<?php $fileName = $_FILES["file1"]["name"]; // The file name $fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder $fileType = $_FILES["file1"]["type"]; // The type of file it is $fileSize = $_FILES["file1"]["size"]; // File size in bytes $fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true if (!$fileTmpLoc) { // if file not chosen echo "ERROR: Please browse for a file before clicking the upload button."; exit(); } if ($fileType=="image/png" || $fileType=="image/gif" || $fileType=="image/jpg") // checking file type echo "ERROR: Only file types .png, .gif or .jpg may be uploaded"; exit(); } if(move_uploaded_file($fileTmpLoc, "image_uploads/$fileName")){ echo "$fileName upload is complete"; } else { echo "move_uploaded_file function failed"; } ?>Thanks. 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. Code: [Select] <?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 I've Got here a code.. it can upload pdf file. but it doesnt accept word documents and powerpoints And i dont know the problem.. Here's The code.. Code: [Select] <?php session_start(); if(isset($_SESSION['uname2'])){ if($_POST['lesson']!=""){ if (($_FILES["Mapfile"]["type"] == "application/acrobat") ||($_FILES["Mapfile"]["type"] == "application/x-pdf") || ($_FILES["Mapfile"]["type"] == "application/pdf") || ($_FILES["Mapfile"]["type"] == "text/pdf") || ($_FILES["Mapfile"]["type"] == "application/powerpoint") || ($_FILES["Mapfile"]["type"] == "application/msword") || ($_FILES["Mapfile"]["type"] == "text/x-pdf") && ($_FILES["Mapfile"]["size"] < 50000)) { if ($_FILES["Mapfile"]["error"] > 0) { echo "Error: " . $_FILES["Mapfile"]["error"] . "<br />"; } else { if (file_exists("upload/" . $_FILES["Mapfile"]["name"])) { echo '<script type="text/javascript"> {alert("'.$_FILES["Mapfile"]["name"] .' already exist");} </script>'; echo '<meta http-equiv="REFRESH" content="0;url=uploadlesson.php">'; } else { move_uploaded_file($_FILES["Mapfile"]["tmp_name"], "upload/" . $_FILES["Mapfile"]["name"]); echo '<script type="text/javascript"> {alert("File Uploaded!");} </script>'; echo '<meta http-equiv="REFRESH" content="0;url=uploadlesson.php">'; $host="localhost"; $username="root"; $password=""; $db_name="dbprof"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $user = $_SESSION['uname2']; $sql="SELECT * FROM registeredprof where Username = '$user'"; $result=mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { $fullname = $row['fullname']; } $title = $_POST['lesson']; $filename = $_FILES['Mapfile']['name']; mysql_query("INSERT INTO lessondb (author, title, filename, status) VALUES ('$fullname', '$title', '$filename', 'offline')"); } } } else { echo '<script type="text/javascript"> {alert("Invalid File! ");} </script>'; echo '<meta http-equiv="REFRESH" content="0;url=uploadlesson.php">'; } } else { echo '<script type="text/javascript"> {alert("Fill out Title!");} </script>'; echo '<meta http-equiv="REFRESH" content="0;url=uploadlesson.php">'; } } ?> Help pls. Hi everybody, I have recently change to a new server, and have some problem with a script for uploading files on my new server. Script is an old which I have updated with new php command, <? not accepted in new server. Scripts shall upload picture to folder in server and write information to database From form script I am saving my upload Code: [Select] <input type="hidden" name="Mode" value="EditMapProperty" /> <input type="hidden" name="Old_Map" value="<?php echo $Property->Get_Map();?>" /> In my php script I do elseif($Mode=="EditMapProperty"): $Map_name = $HTTP_POST_FILES['Map']['name']; $Property=new Property($PropertyID); $ReferenceNo=$Property->Get_ReferenceNo(); if($Map!=""): $DeleteMap=$Dir_MapProperty_Admin."".$Property->Get_Map(); @unlink($DeleteMap); endif; if($Map_name==""): $Map=$Old_Map; else: copy($HTTP_POST_FILES['Map']['tmp_name'], $Dir_MapProperty_Admin."".$ReferenceNo."-".$Map_name); $Map=$ReferenceNo."-".$Map_name; endif; $Property->EditMap($Map); //Update database information echo "<meta http-equiv=\"refresh\" content=\"0;URL=$Redirect2?p=property_details&PropertyID=$PropertyID\">"; () Can any help if there are any PHP problem in this coding. Kind Regards Tom Hi, I can't believe after more then a year I'm stuck with the simplest upload script. Here's my code: <?php if( isset($_POST['upload']) ){ $uploaddir = "uploads/"; $uploadfile = $uploaddir.$_FILES['fileupload']['name']; if( move_uploaded_file($_FILES['fileupload']['tmp_name'], $uploadfile) ){ echo "Upload OK!"; }else{ echo "Upload FAILED!!!"; } }else{ ?> <form action="<?php $_SERVER['PHP_SELF'] ?>" name="upload" method="post"> <input type="file" name="fileupload" /> <input type="submit" name="upload" value="upload" /> </form> <?php } ?> this is the simplest php upload script that you can find, though the least secure one (it's for a local pc project). When I try to upload any file it gets to my else statement. Anyone an idea why? I don't see it Thanks! hello everyone....i am new for this script, hope u guys able to help me. Thank you in advance! im using xampp for localhost, all .php file i place it in xampp\htdocs ( i create a uploads folder outside the htdocs, c:\xampp\uploads) This is the error message i get after i upload the file: Notice: Undefined variable: FILES in C:\xampp\htdocs\upload_file.php on line 15 Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in C:\xampp\htdocs\upload_file.php on line 15 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\phpAE.tmp' to '../uploads/' in C:\xampp\htdocs\upload_file.php on line 15 Your file could not be uploaded because: Something unforeseen happened.. following is my upload_file.php script , i highlighted on line 15: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Upload a File</title> </head> <body> <?php ///Script - 11.4 - upload_file.php /* This script displays and handles an HTML form. this script takes a file upload and stored it on the servers */ if(isset($_POST['submitted'])) { //Handle the form //Try to move the upload file: if (move_uploaded_file ($_FILES['thefile']['tmp_name'],"../uploads/{$FILES['thefile']['name']}")){ print '<p>Your file has been uploaded.</p>'; }else { //problem!! print '<p style="color:red;">Your file could not be uploaded because: '; ///print a message based upon the error: switch ($_FILES['thefile']['error']) { case 1: print 'The file exceeds the upload_max_filesize setting in php.ini'; break; case 2: print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form'; break; case 3: print 'The file was only partially uploaded'; break; case 4: print 'No file was uploaded'; break; case 6: print 'The temporary folder does not exist.'; break; default: print 'Something unforeseen happened.'; break; } print '.</p>'; /////complete the paragraph. } // End of move_uploaded_file()IF } // End of submission IF //Leave PHP and display the form: ?> <form action="upload_file.php" enctype="multipart/form-data" method="post"> <p>Upload a file using this form:</p> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <p><input type="file" name="thefile" /></p> <p><input type="submit" name="submit" value="Upload This File" /></p> <input type="hidden" name="submitted" value="true" /> </form> </body> </html> Hello Im trying to upload mp3s from a form. In the form I have the MAX_FILE_SIZE set at 90000000 (so big to make sure it wasn't my maths). I have checked with phpinfo() and the max upload is set at 8mb. The mp3s will be between 3-4mb in size. I have uploaded small mp3's successfully (440k) so i know the script is working fine, however when i try to upload one at around 3mb it fails, even tried different file sizes in the same ball park size. I suspect the server is blocking them. Any ideas on a fix? Thank you. No script posted as i say its all working. I'm trying to upload a file using ajax, but I have some problems. First I'll show the code : HTML Code: [Select] <form id="foto" action="index.php?action=send" method="post" enctype="multipart/form-data"> <fieldset> <label>Kleur</label> <input id="Binded" type="text" value="ffffff" /> <br /> <label>Upload foto</label> <input id="file" type="file" name="upload" id="upload"/> <a href="index.php?action=foto" id="uploaden">uploaden</a> </fieldset> <fieldset id="audioplayer"> <h3>Kies een soundtrack</h3> <ol id="lijst"> <li id="een" class="active"><a href="assets/audio/Gayla_Peevey-I_Want_a_Hippopotamus_For_Christmas.mp3">Gayla Peevey - I Want a Hippopotamus For Christmas</a> <img class="mute" src="assets/images/mute.png" width="30" height="30" alt="mute"/></li> <li id="twee"><a href="assets/audio/SouthFamilyXmas2005-NuttinForChristmas.mp3">South Family - Nuttin for Christmas</a> <img class="mute" src="assets/images/mute.png" width="30" height="30" alt="mute"/></li> <li id="drie"><a href="assets/audio/southpark_merry_christmas.mp3">South Park - Merry Christmas</a> <img class="mute" src="assets/images/mute.png" width="30" height="30" alt="mute"/></li> </li> </ol> <audio id="audio" preload="auto" autobuffer autoplay src="assets/audio/Gayla Peevey - I Want a Hippopotamus For Christmas.mp3"> </audio> </fieldset> <fieldset id="emailen"> <h3>Email een vriend</h3> <br /> <input type="email" name="email" placeholder="Email" required="true" /> <input type="submit" name="verstuur" id="verstuur" value="Verstuur" /> </fieldset> </form> PHP Code: [Select] function getAjaxContent(){ global $smarty; $dir = "uploads"; $tempdir = $_FILES['upload']["tmp_name"]; $realdir = $dir."/".$_FILES["upload"]["name"]; //$fotoTonen = '<img src="'.$realdir.'" alt="foto upload">'; //$toonFoto = true; if(!is_dir($dir)){ mkdir($dir); } move_uploaded_file($tempdir, $realdir); var_dump($_FILES); return $smarty->fetch('kaart-ajax.htm'); }Here I get the error <b>Notice</b>: Undefined index: upload in <b>/Applications/MAMP/htdocs/2011-2012/kerstkaart/includes/index.php</b> on line <b>16</b><br /> and the var_dump gives an empty array AJAX Code: [Select] $('input[name=foto]').change(function(event){ $.ajax({ url:"index.php?action=foto", data: $('input[type=file]').val(), type: "POST", timeout: 3000, error: function(jqXHR, textStatus,errorThrown){ alert("probleem met ajax "+textStatus); }, success: function(data, textStatus, jqXHR){ alert("jaja "+data); canvas.width = canvas.width; context.drawImage(image, 0, 0); image.src = 'uploads/'+data; } }); event.preventDefault(); }); What I try to do here is getting the value of the selected file and redraw my canvas ( HTML5 ) so that the chosen pic is the background. The file i upload to test is a gif of 4KB, so not that big. Can anyone help me with this confusing problem ? Thanks, |