Basically I'm trying to create a simple image uploading script. (for now). I'm trying to learn php, and this is my first obsticle. I can make the html form, no problem, which brings the users file path input and redirects to the page with this code: <?php if (($_FILES["file"]["type"] == "image/gif") or ($_FILES["file"]["type"] == "image/jpeg") or (($_FILES["file"]["type"] == "image/bmp") or ($_FILES["file"]["type"] == "image/png") and ($_FILES["file"]["size"] < 1024)) { echo "Upload: " . $_FILES["file"]["name"]. "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } elseif ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br>"; } if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . "Already exists. Rename and reupload file. "; } else { move_uploaded_file($FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Location: " . "upload/" . $_FILES["file"]["name"]; } ?> Code (markup): This is similar to the tutorial on the w3schools but has some differences. I'd like to know what's causing this to throw an error. (I'll make changes, it'll just throw a different error). As far as I can tell it's should work, which obviously isn't the case. Also, when I do/did get it to work, it'll display the info (name, size, location, etc) only it won't actually upload. I'll check the /upload directory and nothing's there. sorry for the novice problem, but I'm new at this. Hoping for a little guidance. Thanks,
You need to make the directory. You can't just put files in a folder that does not exist. In your if() statement when it goes to the else because the folder does not exist you need to create it with mkdir().
This might help you out a bit. Zend usually has pretty good sample and tutorials. http://www.zend.com/manual/features.file-upload.php And don't forget to check the perms on the directory.
Hi Friend, Recenty I did a similiar works. Check it here http://www.webmarketreports.com/uploadImage2.php if it is what you want, let me know I can send you the script
Something simlar to that. I'm learning php, and yes, I need something like that. I'd appreciate it if you could send the script my way - so I can at least study how you made it. Even so, I'd like to figure out what I need to complete my own.
^ Thanks. I'll mess arnound with them. <?php if( (($_FILES["file"]["type"] == "image/gif") or ($_FILES["file"]["type"] == "image/jpeg") or ($_FILES["file"]["type"] == "image/bmp") or ($_FILES["file"]["type"] == "image/png")) and ($_FILES["file"]["size"] < 1024)) { echo "Upload: " . $_FILES["file"]["name"]. "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } elseif ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br>"; } if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . "Already exists. Rename and reupload file. "; } elseif(move_uploaded_file($FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"])) { echo "Location: " . "upload/" . $_FILES["file"]["name"]; } else { # didn't work } ?> Code (markup): ^ That's what I have now, which apparently fixed my problem. However, it still doesn't actually save the image to the host. Any ideas?
hi, here is one class i do use generally. i have included a simple implementation. <?php //define the class or include it from external file //i have defined it here. class uploader { var $file; var $path; var $language; var $acceptable_file_types; var $error; var $errors; // Depreciated (only for backward compatability) var $accepted; var $max_filesize; var $max_image_width; var $max_image_height; function uploader ( $language = 'en' ) { $this->language = strtolower($language); $this->error = ''; } /** * void max_filesize ( int size); * * Set the maximum file size in bytes ($size), allowable by the object. * NOTE: PHP's configuration file also can control the maximum upload size, which is set to 2 or 4 * megs by default. To upload larger files, you'll have to change the php.ini file first. * * @param size (int) file size in bytes * */ function max_filesize($size){ $this->max_filesize = (int) $size; } /** * void max_image_size ( int width, int height ); * * Sets the maximum pixel dimensions. Will only be checked if the * uploaded file is an image * * @param width (int) maximum pixel width of image uploads * @param height (int) maximum pixel height of image uploads * */ function max_image_size($width, $height){ $this->max_image_width = (int) $width; $this->max_image_height = (int) $height; } /** * bool upload (string filename[, string accept_type[, string extension]]); * * Checks if the file is acceptable and uploads it to PHP's default upload diretory * * @param filename (string) form field name of uploaded file * @param accept_type (string) acceptable mime-types * @param extension (string) default filename extenstion * */ function upload($filename='', $accept_type='', $extention='') { $this->acceptable_file_types = trim($accept_type); // used by error messages if (!isset($_FILES) || !is_array($_FILES[$filename]) || !$_FILES[$filename]['name']) { $this->error = $this->get_error(0); $this->accepted = FALSE; $result['result'] = false; $result['message'] = $this->error; return $result; } // Copy PHP's global $_FILES array to a local array $this->file = $_FILES[$filename]; $this->file['file'] = $filename; // Initialize empty array elements if (!isset($this->file['extention'])) $this->file['extention'] = ""; if (!isset($this->file['type'])) $this->file['type'] = ""; if (!isset($this->file['size'])) $this->file['size'] = ""; if (!isset($this->file['width'])) $this->file['width'] = ""; if (!isset($this->file['height'])) $this->file['height'] = ""; if (!isset($this->file['tmp_name'])) $this->file['tmp_name'] = ""; if (!isset($this->file['raw_name'])) $this->file['raw_name'] = ""; // test max size if($this->max_filesize && ($this->file["size"] > $this->max_filesize)) { $this->error = $this->get_error(1); $this->accepted = FALSE; $result['result'] = false; $result['message'] = $this->error; return $result; } if(stristr($this->file["type"], "image")) { /* IMAGES */ $image = getimagesize($this->file["tmp_name"]); $this->file["width"] = $image[0]; $this->file["height"] = $image[1]; // test max image size if(($this->max_image_width || $this->max_image_height) && (($this->file["width"] > $this->max_image_width) || ($this->file["height"] > $this->max_image_height))) { $this->error = $this->get_error(2); $result['result'] = false; $result['message'] = $this->error; return $result; } // Image Type is returned from getimagesize() function switch($image[2]) { case 1: $this->file["extention"] = ".gif"; break; case 2: $this->file["extention"] = ".jpg"; break; case 3: $this->file["extention"] = ".png"; break; case 4: $this->file["extention"] = ".swf"; break; case 5: $this->file["extention"] = ".psd"; break; case 6: $this->file["extention"] = ".bmp"; break; case 7: $this->file["extention"] = ".tif"; break; case 8: $this->file["extention"] = ".tif"; break; default: $this->file["extention"] = $extention; break; } } elseif(!ereg("(\.)([a-z0-9]{3,5})$", $this->file["name"]) && !$extention) { // Try and autmatically figure out the file type // For more on mime-types: http://httpd.apache.org/docs/mod/mod_mime_magic.html switch($this->file["type"]) { case "text/plain": $this->file["extention"] = ".txt"; break; case "text/richtext": $this->file["extention"] = ".txt"; break; default: break; } } else { $this->file["extention"] = $extention; } // check to see if the file is of type specified if($this->acceptable_file_types) { if(trim($this->file["type"]) && (stristr($this->acceptable_file_types, $this->file["type"]) || stristr($this->file["type"], $this->acceptable_file_types)) ) { $this->accepted = TRUE; } else { $this->accepted = FALSE; $this->error = $this->get_error(3); $result['result'] = false; $result['message'] = $this->error; return $result; } } else { $this->accepted = TRUE; $result['result'] = true; return $result; } return $result; } /** * bool save_file ( string path[, int overwrite_mode] ); * * Cleans up the filename, copies the file from PHP's temp location to $path, * and checks the overwrite_mode * * @param path (string) File path to your upload directory * @param overwrite_mode (int) 1 = overwrite existing file * 2 = rename if filename already exists (file.txt becomes file_copy0.txt) * 3 = do nothing if a file exists * */ function save_file($path, $overwrite_mode="3"){ if ($this->error) { return false; } if (strlen($path)>0) { if ($path[strlen($path)-1] != "/") { $path = $path . "/"; } } $this->path = $path; $copy = ""; $n = 1; $success = false; if($this->accepted) { // Clean up file name (only lowercase letters, numbers and underscores) $this->file["name"] = ereg_replace("[^a-z0-9._]", "", str_replace(" ", "_", str_replace("%20", "_", strtolower($this->file["name"])))); // Clean up text file breaks if(stristr($this->file["type"], "text")) { $this->cleanup_text_file($this->file["tmp_name"]); } // get the raw name of the file (without its extenstion) if(ereg("(\.)([a-z0-9]{2,5})$", $this->file["name"])) { $pos = strrpos($this->file["name"], "."); if(!$this->file["extention"]) { $this->file["extention"] = substr($this->file["name"], $pos, strlen($this->file["name"])); } $this->file['raw_name'] = substr($this->file["name"], 0, $pos); } else { $this->file['raw_name'] = $this->file["name"]; if ($this->file["extention"]) { $this->file["name"] = $this->file["name"] . $this->file["extention"]; } } switch((int) $overwrite_mode) { case 1: // overwrite mode if (@copy($this->file["tmp_name"], $this->path . $this->file["name"])) { $success = true; } else { $success = false; $this->error = $this->get_error(5); } break; case 2: // create new with incremental extention while(file_exists($this->path . $this->file['raw_name'] . $copy . $this->file["extention"])) { $copy = "_copy" . $n; $n++; } $this->file["name"] = $this->file['raw_name'] . $copy . $this->file["extention"]; if (@copy($this->file["tmp_name"], $this->path . $this->file["name"])) { $success = true; } else { $success = false; $this->error = $this->get_error(5); } break; default: // do nothing if exists, highest protection if(file_exists($this->path . $this->file["name"])){ $this->error = $this->get_error(4); $success = false; } else { if (@copy($this->file["tmp_name"], $this->path . $this->file["name"])) { $success = true; } else { $success = false; $this->error = $this->get_error(5); } } break; } if(!$success) { unset($this->file['tmp_name']); $result['result'] = false; $result['message'] = $this->error; }else{ $result['result'] = true; $result['file'] = $this->file["name"]; $result['message'] = 'File uploaded Successfully'; } return $result; } else { $this->error = $this->get_error(3); $result['result'] = false; $result['message'] = $this->error; return $result; } } /** * string get_error(int error_code); * * Gets the correct error message for language set by constructor * * @param error_code (int) error code * */ function get_error($error_code='') { $error_message = array(); $error_code = (int) $error_code; $error_message[0] = "No file was uploaded"; $error_message[1] = "Maximum file size exceeded. File may be no larger than " . $this->max_filesize/1000 . " KB (" . $this->max_filesize . " bytes)."; $error_message[2] = "Maximum image size exceeded. Image may be no more than " . $this->max_image_width . " x " . $this->max_image_height . " pixels."; $error_message[3] = "Only " . str_replace("|", " or ", $this->acceptable_file_types) . " files may be uploaded."; $error_message[4] = "File '" . $this->path . $this->file["name"] . "' already exists."; $error_message[5] = "Permission denied. Unable to copy file to '" . $this->path . "'"; // for backward compatability: $this->errors[$error_code] = $error_message[$error_code]; return $error_message[$error_code]; } /** * void cleanup_text_file (string file); * * Convert Mac and/or PC line breaks to UNIX by opening * and rewriting the file on the server * * @param file (string) Path and name of text file * */ function cleanup_text_file($file){ // chr(13) = CR (carridge return) = Macintosh // chr(10) = LF (line feed) = Unix // Win line break = CRLF $new_file = ''; $old_file = ''; $fcontents = file($file); while (list ($line_num, $line) = each($fcontents)) { $old_file .= $line; $new_file .= str_replace(chr(13), chr(10), $line); } if ($old_file != $new_file) { // Open the uploaded file, and re-write it with the new changes $fp = fopen($file, "w"); fwrite($fp, $new_file); fclose($fp); } } } //set the path where uploaded files will be stored //this directory must have enough write permissions //keep permissions 777 if you are not sure $path = "uploads/"; if( isset($_POST['Submit'] ) ){ $u = new uploader; $u->max_filesize( 100000 ); //max file size in bytes $log = $u->upload( "userfile" ); // userfile is name of field in the form // you can set two more parameters for this function //@param filename (string) form field name of uploaded file // @param accept_type (string) acceptable mime-types // @param extension (string) default filename extenstion // check definition in class for exact function usage if( $log['result'] ){ //if file is uploaded $log = $u->save_file( $path ); //can have 2 more parameters } if( $log['result'] ){ echo 'You file is uploaded to <a href="'.$path.$log['file'].'">'.$path.$log['file'].'</a>'; }else{ echo $log['message']; } } ?> <!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=iso-8859-1" /> <title>File Uploader</title> </head> <body><br /> <form enctype="multipart/form-data" action="upload.php" method="post"> <input name="userfile" type="file" /> <input type="submit" name="Submit" value="Upload File" /> </form> </body> </html> Code (markup):
Oh god dont use thats script ur asking ti get hacked i reccommend looking into PEAR::HTTP_Upload or flash upload or even perl btw php/apache is totaly unsuited for large file uploads/downloads see my sig, the site in question uses several hundred GB a day over several servers, we had to scrap apache simply because it couldnt handle the load DONT trust phps $_FILES global it can all be faked, use linuxes built in functions such to determine the mime-types, md5 sums, filesize and above all make sure files are well outside the web tree and have minimal permissions