I would need some advice/assistance here. I'm able to upload image and successfully move into (upload folder), however, every image has two different filename together (tmp_name
and $_FILES["file"]["name"]
) after uploaded, would appreciate if anyone can assist here. Thanks.
<form id="form" action="espaceupload.php" method="post" enctype="multipart/form-data">
<input id="uploadImage" type="file" accept="image/*" name="file" /><br>
<input id="button" type="submit" value="Preview">
</form>
<?php
$valid_exts = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
$max_size = 5000000 * 1024; // max file size
$path = "upload/" . $_FILES["file"]["name"]; // upload directory
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if( ! empty($_FILES['file']) ) {
$imagedata = addslashes (file_get_contents($_FILES['file']['tmp_name']));
$imagename = ($_FILES['file']['tmp_name']);
$imagetype =($_FILES['file']['type']);
$imagesize= $_FILES['file']['size'];
// get uploaded file extension
$ext = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));
// looking for format and size validity
if (in_array($ext, $valid_exts) AND $_FILES['file']['size'] < $max_size) {
$path = $path . uniqid(). '.' .$ext;
// move uploaded file from temp to uploads directory
if (move_uploaded_file($_FILES["file"]["tmp_name"],$path)) {
echo "<img src='$path' />";
$sql1= mysql_query("INSERT INTO dumimage(name,image,type,email,storename)values('$imagename','$imagedata','$imagetype','$user_check','$user_store')");
}
} else {
echo 'Invalid file!';
}
} else {
echo 'File not uploaded!';
}
} else {
echo 'Bad request!';
}
echo ($imagesize/1024).'KB';
?>
You need to read the file contents after you've uploaded the file, NOT before (and not using $_FILES['file']['tmp_name']
).
And place it after move_uploaded_file
, like this:
if (move_uploaded_file($_FILES["file"]["tmp_name"],$path)) {
$imagedata = addslashes(file_get_contents($path));
// save to DB
$_FILES['file']['name']
contains the name of the file that was uploaded, for instance myImage.jpg
. In other words, this is the name given by the user that uploaded the file.
$_FILES['file']['tmp_name']
contains the temporary file location. This is where PHP is keeping the file, until you move it somewhere else. For instance /tmp/php/php1h4j1o
.
You should read the documentation on PHP.net.