Jump to content

Uploading image file from form


jason360

Recommended Posts

Hey guys,

 

Stuck on this for a while.  Not too sure what I am doing wrong. 

 

I am just trying to submit an image file(jpg,png,gif) via a form to my uploader file.  It seems like only the file name is coming through and not the actual file.

 

Any help is much appreciated!

 

Thanks!

 

Upload form:

<?php 

include("image_upload_script.php");

?>
        
<form enctype="multipart/form-data" class="clearfix" action="" method="post">
						<?php
                            
                            if($_SESSION['msg']['login-err'])
                            {
                                echo '<div class="err">'.$_SESSION['msg']['login-err'].'</div>';
                                unset($_SESSION['msg']['login-err']);
                            }
                        ?>
Choose your file here:
<input name="uploaded_file" type="file"/><br /><br />
<input type="submit" name="submit" value="Upload It" class="" />
</form>
 

PHP Uploader file being submitted to (image_upload_script.php):

session_start();
$pid ='1000';
if($_POST['submit']=='Upload It')
{

// Access the $_FILES global variable for this specific file being uploaded
// and create local PHP variables from the $_FILES array of information
$fileName = $_POST["uploaded_file"]["name"]; // The file name
$fileTmpLoc = $_POST["uploaded_file"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_POST["uploaded_file"]["type"]; // The type of file it is
$fileSize = $_POST["uploaded_file"]["size"]; // File size in bytes
$fileErrorMsg = $_POST["uploaded_file"]["error"]; // 0 for false... and 1 for true
$kaboom = explode(".", $fileName); // Split file name into an array using the dot
$fileExt = end($kaboom); // Now target the last array element to get the file extension
$fileName = $pid.".".$fileExt; //rename
// START PHP Image Upload Error Handling --------------------------------------------------
if (!$fileTmpLoc) { // if file not chosen
   $err[] = 'ERROR: Please browse for a file before clicking the upload button.';
} else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes
	$err[] = 'ERROR: Your file was larger than 5 Megabytes in size.';
    unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
} else if (!preg_match("/.(gif|jpg|png)$/i", $fileName) ) {
     // This condition is only if you wish to allow uploading of specific file types    
     $err[] = 'ERROR: Your image was not .gif, .jpg, or .png.';
     unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
} else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
	$err[] = 'ERROR: An error occured while processing the file. Try again.';
}
// END PHP Image Upload Error Handling ---------------------------------
// Place it into your "uploads" folder mow using the move_uploaded_file() function
$moveResult = move_uploaded_file($fileTmpLoc, "uploads/$fileName");
// Check to make sure the move result is true before continuing
if ($moveResult != true) {
	$err[] = 'ERROR: File not uploaded. Try again.';
    unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
}
// more code below this 
Link to comment
https://forums.phpfreaks.com/topic/284868-uploading-image-file-from-form/
Share on other sites

while uploading files use $_FILES instead of $_POST for file data

$fileName = $_POST["uploaded_file"]["name"]; // The file name
$fileTmpLoc = $_POST["uploaded_file"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_POST["uploaded_file"]["type"]; // The type of file it is
$fileSize = $_POST["uploaded_file"]["size"]; // File size in bytes

the above code should be

$fileName = $_FILES["uploaded_file"]["name"]; // The file name
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["uploaded_file"]["type"]; // The type of file it is
$fileSize = $_FILES["uploaded_file"]["size"]; // File size in bytes

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.