Image not uploading to web server - php

I am having some difficulty uploading an image to a folder on my web server. Here's my code:
HTML:
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="Upload Image" name="upload">
</form>
PHP:
<?php
if (isset($_POST["upload"])) {
$name = $_FILES["image"]["name"];
$type = $_FILES["image"]["type"];
$size = $_FILES["image"]["size"];
$temp = $_FILES["image"]["tmp_name"];
$error = $_FILES["image"]["error"];
$target_file = "/profiles/images/$name";
move_uploaded_file($temp, $target_file);
}
?>
I've tried echoing out the file name ($name), but it doesn't return anything at all. It's blank for some reason. This is when I try to upload an image. When I echo $target_file, I get this "/profiles/images/", the $name part is not included for some reason.

Check your max upload file size. If in the settings the size is smaller than the file you are trying to upload, nothing is send.

This is happens in those cases when we try to upload very heavy size image, Please try to check your maximum file size, and please for testing try to upload maximum 100kb size of pic through this code. Hope this will work.

Related

PHP move_uploaded_file() doesn't upload .zip of 3MB to server

I try to upload a file with PHP and I use the function "move_uploaded_file()" for it. Everything works well, until I try to upload a zipped CSV file. The file itself is 50MB, but once it is zipped, it's just 3MB. I can upload files until 5MB to the server, so in the case of the zip file, I thought this would work, but it doesn't.
* Uploading a zipped CSV file of 100kb does work,
* I did delete 1/3 of the lines from the CSV file, the CSV file is 28MB, and this I could upload (zipped it was 1.6MB),
* The zipped file of 3MB doesn't appear in the folder on the server. The zipped files of 100KB and 1.6MB do appear there,
* I didn't get an error message, but it does echo "Error uploading!".
Did someone face the same problems, or does someone knows a solution? Let me know! This is basicly my code;
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if (isset($_POST["submit"])) {
$file = $_FILES["file"];
$filename = $file["name"];
$target_dir = "data/";
$target_file = $target_dir . basename($filename);
$file_type = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if (move_uploaded_file($file["tmp_name"], $target_file)) {
echo "Done uploading!";
} else {
echo "Error uploading!";
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<div class="custom-file mb-3">
<input type="file" class="custom-file-input" id="file" name="file" required accept=".csv,.zip" />
</div>
<p class="text-right">
<button class="btn btn-primary" name="submit" type="submit">Upload!</button>
</p>
</form>
Let me know if you need something. All the help and ideas are appericiated. Thanks!
Edit;
I did got an error in $_FILES['file']['error'];
I got error "1", which reffers to: 'The uploaded file exceeds the upload_max_filesize directive in php.ini'
https://www.php.net/manual/en/features.file-upload.errors.php
I did "echo phpinfo();", and found out that upload_max_filesize is only 2MB instead of the 5MB that I thought I configurated.

Using PHP code and rename an image file before uploading to XAMPP database?

I am new to php and is it possible to rename an image file before uploading to the database?
EDIT: would be using a form to upload the file to a database.
<input type="file" name="image">
<input type="submit" name="upload" value="Add" action="viewpage.php">
EDIT: IMAGE OF DATABASE:
The second image above still shows the original image file name in the database while the image name in the directory already changed.
Yor can try this..
<?php
if(isset($_POST['submit_btn']))
{
$tmp_file = $_FILES['uploadedfile']['tmp_name'];
$ext = pathinfo($_FILES["uploadedfile"]["name"], PATHINFO_EXTENSION);
$rand = md5(uniqid().rand());
$post_image = $rand.".".$ext;
move_uploaded_file($tmp_file,"../post_imgs/".$post_image);
}
?>

make a upload field using php and send the url to database

I want to create a form to submit an image file in the front-end and send it to the wordpress upload directory and database using php ,but my php says the file is a image but dont upload it :
<div id="recibo">
<form action="" method="post" enctype="multipart/form-data">
<p>upload do recibo:
<input type="file" name="pictures[]" />
<input type="submit" value="Enviar" />
</p>
</form>
</div>
php:
<?php
$target_dir = "wp-content/uploads/recibos";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
There are a few things wrong here.
Firstly, the file input's name attribute is name="pictures[]" yet you are using $_FILES["fileToUpload"], those must match.
So rename it: name="fileToUpload[]"
Sidenote: Using brackets implies an array to upload multiple files; you don't need it here since the rest of your code doesn't support multiple files uploading.
Change it to name="fileToUpload"
Then your folder path:
$target_dir = "wp-content/uploads/recibos";
You have no directory seperator after recibos
This would translate to recibosFILE.JPG rather than recibos/FILE.JPG.
Change it to:
$target_dir = "wp-content/uploads/recibos/";
Make sure the folder you are uploading to has the correct permissions to write to it.
Another thing: I can't see how if(isset($_POST["submit"])) {...} would work here, since your submit button does not have the name attribute to match it with.
<input type="submit" value="Enviar" /> to <input name="submit" type="submit" value="Enviar" />
Special note:
If you do want to upload multiple files, then you will need to find yourself another script to do that and this isn't what your question is about.
See also:
PHP change the maximum upload file size
to increase upload size
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.

How to upload images to a domain at webhost using php?

What should I do if I want to upload an image to a file system such as a folder in my domain? The thing is, I've tried uploading image on a localhost and it works fine but when i did it in my domain it doesn't work anymore. Don't really know what's the problem here. any help will do! Thanks anyway.
<form action="adminUploadTry.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" >
<input type="submit" name="submit" value="Upload" >
</form>
<?php
if(isset($_POST['submit'])){
$image_name = $_FILES['image']['name'];
$image_type = $_FILES['image']['type'];
$image_size = $_FILES['image']['size'];
$image_tmp_name = $_FILES['image']['tmp_name'];
if($image_name==''){
echo "You forgot to select an image. Please choose one!";
exit();
}
else
move_uploaded_file(image_tmp_name, "/home/stagcon2/public_html/StagConnect/admin/pictures/$image_name");
echo "Image Succesfully Uploaded!";
echo "<img src='/home/stagcon2/public_html/StagConnect/admin/pictures/$image_name'>";
}
?>
by the way, the problem here is that after i uploaded an image it will say "successfully uploaded" but when I check my folder the image was not saved.
May be you forget $ sign in image_tmp_name at
move_uploaded_file($image_tmp_name, "/home/stagcon2/public_html/StagConnect/admin/pictures/$image_name");
See if you need to increase the value of upload_max_filesize and post_max_size in your php.ini :
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M

Image upload failing in PHP

So I'm trying to set up an image upload from a form via php on my localhost and after running the code and connecting to the database okay, I'm getting the error for the upload. Since all of the other parts of the form are working after a section by section check, I'll just add the html for the upload input and its relevant php script.
<input type="file" name="image" id="image-select" />
And the portion of the php that has to deal with the image upload and verification after upload:
$image = $_FILES ['image']['name'];
$type = #getimagesize ($_FILES ['image']['tmp_name']);
//Never assume image will upload okay
if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code " . $_FILES['image']['error']);
}
//Where the file will be placed
$target_path = "uploads/";
// Add the original filename to target path
$path= $target_path . basename($image);
// Check if the image is invalid before continuing
if($type === FALSE || !($type[2] === IMAGETYPE_TIFF || $type[2] === IMAGETYPE_JPEG || $type[2] === IMAGETYPE_PNG)) {
echo '<script "text/javascript">alert("This is not a valid image file!")</script>';
die("This is not a valid image file!");
} else {
move_uploaded_file($_FILES['image']['tmp_name'], $path);
}
So error appears once the code hits the upload process. I was attempting to upload a small PNG file The relevant code I added is also in their respective orders when they appear in the script.
can you please put error here. If error means that after submit page file is not being upload. then please check your form enctype. see below an example
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image" id="image-select" />
<input type="submit" value="Submit">
</form>
Just a wild stab in the dark here as you haven't provided your form tag but you have remembered the enctype attribute haven't you?
Sorry I don't have 50 reputation otherwise I would ask this as a comment.
<form enctype='multipart/form-data' action=''>
<input type="file" name="image" id="image-select" />
</form>
Also you should check if the file uploaded OK before calling getimagesize() (not that this would be the source of your issue)
if you are using PHP5 and Imagick, then you can try to use something like the following:
$image->readImageFile($f);

Categories