PHP - File-Uploader - php

I want to create a file-uploader with php.
Does anybody know where the problem is. I am writing this code for the 3rd time, but it does not work...
Can anybody help me???
Thank you!!!
HTML-File:
<html>
<head>
<meta charset="utf-8" />
<title>File Uploader</title>
</head>
<body>
<form action="upload_files.php" method="post" enctype="multipart/formdata">
<input type="file" name="file" />
<input type="submit" value="upload" />
</form>
</body>
</html>
PHP-File:
<?php
if (isset($_FILES['file'])) {
$file = $_FILES['file'];
$file_name = $file['name'];
$file_tmp = $file['file_tmp'];
$file_size = $file['size'];
$file_error = $file['error'];
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
$allowed = array('txt', 'jpg');
if (in_array($file_ext, $allowed)) {
if ($file_error === 0) {
$file_new_name = uniqid('', true) . '.' . $file_ext;
$file_dir = 'uploads/' . $file_new_name;
if (move_uploaded_file($file_tmp, $file_dir)) {
echo $file_dir;
}
}
}
}
?>

You have done mistake here.. enctype="multipart/formdata"
Change this to enctype="multipart/form-data"
<form action="upload_files.php" method="post" enctype="multipart/form-data">
</form>
Check This File Upload

Related

Upload file code don't upload image in PHP

I was trying to upload a folder inside my htdocs folder in XAMPP.
I followed the rules of move_uploaded_file still did not work.
Here's my current code:
<?php
if(isset($_POST['submit'])){
$allowed_ext = array('png', 'jpg', 'jpeg', 'gif');
if(!empty($_FILES['upload']['name'])){
print_r($_FILES);
$file_name = $_FILES['upload']['name'];
$file_size = $_FILES['upload']['size'];
$file_tmp = $_FILES['upload']['tmp_name'];
$target_dir = "uploads/{$file_name}";
// Get file ext
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
// Validate file ext
if(in_array($file_ext, $allowed_ext)) {
// verify size
if($file_size <= 1000000) { // 1000000 bytes = 1MB
// Upload file
move_uploaded_file($file_tmp, $target_dir);
$message = '<p style="color: green;">File uploaded!</p>';
} else {
$message = '<p style="color: red;">File to large</p>';
}
} else {
$message = '<p style="color: red;">Invalid file type</p>';
}
} else {
$message = '<p style="color: red;">Please choose a file</p>';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload</title>
</head>
<body>
<?php echo $message ?? null; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="upload" />
<input type="submit" value="submit" name="submit" />
</form>
</body>
</html>
Right now the images are not getting move to the uploads folder inside my current directory.
Any idea why?
Try this:
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 1000000){
$errors[]='File size must be excately 1 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"uploads/".$file_name); //create a folder 'uploads' in your folder
echo "Success";
}else{
print_r($errors);
}
}
?>

Can't get filesize() from 20 Mb file

I'm setting an application that reads information from a ts video file. If the file is 10 Kb, he function works perfectly, but when the file is much longer (around 20.478 KB) it gets frozen, and I can't get any information. No filesize(), neither files() or anything. I tried almost everything but I'm out of ideas.
HTML code:
<!DOCTYPE html>
<html>
<head>
<title>Proyecto</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="leerBytes.php" enctype="multipart/form-data">
Choose a file: <br>
<input type="file" name="file" id="file"> <br>
<input type="submit" name="button" id="button" value="Send" onclick="post"> <br>
</form>
</body>
</html>
PHP code: (leerBytes.php)
<?php
if(isset($_POST["button"])){
echo "File sent <br>";
$file = $_FILES['file'];
echo "<br>";
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode ('.',$fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('ts');
if (in_array($fileActualExt, $allowed)) {
if($fileError === 0){
$fileNameNew = uniqid('',true).".".$fileActualExt;
$fileDestination = 'uploads/' .$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
$byte = new leerBytes();
$byte ->leerByte($fileDestination);
}else{
echo 'Error.';
}
}else{
echo 'Wrong file.';
}
}
class leerBytes{
function leerByte($file){
$filesize = filesize($file);
echo $filesize;
$fileopen = fopen($file, "rb") or die("Can't get fileopen");
$bytesize = 8;
if($fileopen){
$header = null;
$body = null;
echo "There's data";
$buffer = fgets($fileopen, 4096);
echo $buffer;
fclose($fileopen);
} else {
echo "Error";
}
}
}
?>
I expect to get the filesize, or the information from the file in an array.
I leave here some files as example: https://drive.google.com/drive/folders/1Hge4w8cIZBRm3TskABWcQZSoFFVUMx0-?usp=sharing

i am not able to see uploaded image from simple html and php code

i am making a simple image upload event or function from simple php and html code,although i dont get any errors, i cant see my uploaded image in my folder named 'uploads',what should i do?
....html
<!DOCTYPE html>
<html>
<head>
<title>prasad</title>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit" name="submit">upload</button>
</form>
</body>
</html>
...html
...php
<?php
if (isset($_POST['submit'])){
$file = $_FILES['file'];
print_r($file);
echo "<br>";
$fileName = $_FILES['file']['name'];
$fileType=$_FILES['file']['type'];
$file_temp_name=$_FILES['file']['tmp_name'];
$file_error=$_FILES['file']['error'];
$file_size=$_FILES['file']['size'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg','jpeg','png','pdf');
if (in_array($fileActualExt, $allowed)) {
if ($file_error === 0) {
if ($file_size<1000000){
$fileNameNew = uniqid('',true).".".$fileActualExt;
$fileDestination = 'uplaods/'.$fileNameNew;
move_uploaded_file($file_temp_name, $fileDestination);
header("Location: pp.php?success");
}else {
echo "this is too big file";
}
# code...
}else{
echo "there was an error ..!";
}
}else{
echo "<h1>you couldnt choose any file..</h1>";
}
}
the only error is , i aint able to see my uploaded image

upload image and showing the info

I was trying to upload a picture file and I wanted to display the information of that picture. But my code is not working, I'm not sure what's the problem, I hope someone could point out what am I doing wrong.
<!DOCTYPE html>
<html>
<head>
<title>Upload Image</title>
</head>
<body>
<form action="upload_image.php" action="post" enctype="multipart/form-data">
Select image: <input type="file" name="image">
<input type="submit" name="upload" value="Upload Now">
</form>
<?php
if(isset($_POST['upload'])){
echo $image_name = $_FILES['image']['name'];
echo $image_type = $_FILES['image']['type'];
echo $image_size = $_FILES['image']['size'];
echo $image_tmp_name = $_FILES['image']['tmp_name'];
if($image_name == ""){
echo "<script>alert('Please select an image!')</script>";
exit();
}
}
?>
</body>
</html>
ps: I'm still new to php and file upload, so please go easy on me..Thank you in advance
The problem appears to be in the fact that you didn't specify any location to where to upload it, neither the file type. Here is the code with Extension verification, Size, and moving the file in a folder called images.
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions = array("jpeg","jpg","png");
if(in_array($file_ext,$extensions )=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file."
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>

Upload Multiple Files in PHP & INSERT path to MySQL

I've setup a form to upload multiple files from a PHP script and then insert it into the Database with path. Here's my code
<form action="" method="post" enctype="multipart/form-data">
<tr class='first'>
<td>Property Image : </td>
<td>
<input type="file" name="pic_upload[]" >
<input type="file" name="pic_upload[]" >
<input type="file" name="pic_upload[]" >
</td>
</tr>
<tr class='first'>
<td> </td><td><input type="submit" name="create" value="Add" /></td>
</tr>
</form>
<?php
if(isset($_POST['create'])) {
$path = "images/";
for ($i=0; $i<count($_FILES['pic_upload']['name']); $i++) {
$ext = explode('.', basename( $_FILES['pic_upload']['name'][$i]));
$path = $path . md5(uniqid()) . "." . $ext[count($ext)-1];
move_uploaded_file($_FILES['pic_upload']['tmp_name'][$i], $path);
}
$sql = "INSERT INTO post (`image`) VALUES ('$path');";
$res = mysqli_query($con,$sql) or die("<p>Query Error".mysqli_error()."</p>");
echo "<p>Post Created $date</p>";
}
?>
The Script runs successfully, but at the database end inside the column it looks like this.
images/8de3581eb72ee7b39461df48ff16f4a3.jpg024fae942ae8c550a4bd1a9e028d4033.jpg380cc327df25bc490b83c779511c015b.jpg
Help me out with this please
Move $path = "images/"; inside the for loop. Otherwise you are appending to the filename without resetting it after each iteration. Actually, you don't need to use a variable for that prefix at all. You can simply write 'images/' . md5(uniqid()) . "." . $ext[count($ext)-1] immediately.
To write the values to the database, you can either run the query in each iteration as well or add the paths to an array which is transformed to the comma-separated insertion list according to the SQL syntax.
Here is what worked for me:everything contained in the for loop then just return the $fileDest
<?php
if(isset($_POST['submit'])){
$total = count($_FILES['files']['tmp_name']);
for($i=0;$i<$total;$i++){
$fileName = $_FILES['files']['name'][$i];
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
$newFileName = md5(uniqid());
$fileDest = 'filesUploaded/'.$newFileName.'.'.$ext;
if($ext === 'pdf' || 'jpeg' || 'JPG'){
move_uploaded_file($_FILES['files']['tmp_name'][$i], $fileDest);
}else{
echo 'Pdfs and jpegs only please';
}
}
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form class="" action="test.php" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple>
<button type="submit" name="submit">Upload</button>
</form>
</body>
</html>
$files = array_filter($_FILES['lamp']['name']);
$total = count($files);
$path = "./asset/save_location/";
// looping for save file to local folder
for( $i=0 ; $i < $total ; $i++ ) {
$ext = explode('.', basename( $_FILES['lamp']['name'][$i]));
$tmpFilePath = $_FILES['lamp']['tmp_name'][$i];
if ($tmpFilePath != ""){
$newFilePath = $path .date('md').$i.".".$ext[count($ext)-1]; //save lampiran ke folder $path dengan format no_surat_tgl_bulan_nourut
// upload success
if(move_uploaded_file($tmpFilePath,$newFilePath)) {
$success="file uploaded";
}
}
} //end for
//looping for save namefile to database
for ($i=0; $i<count($_FILES['lamp']['name']); $i++) {
$ext = explode('.', basename( $_FILES['lamp']['name'][$i]));
$path = $path .date('md') .$i. "." . $ext[count($ext)-1].";";
$save_dir=substr($path, 22,-1);
}//end for
var_dump($save_dir);die();

Categories