Upload file code don't upload image in PHP - 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);
}
}
?>

Related

The requested URL /learnPHP2/upload.php was not found on this server

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-`enter code here`scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>file upload</title>
</head>
<body>
<form action="upload.php" method ="POST" enctype="multipart/form-data">
Uploadfile <input type="file" name="userfile">
<input type="submit">
</form>
<?php
// Check if the form was submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Check if file was uploaded without errors
if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$filename = $_FILES["photo"]["name"];
$filetype = $_FILES["photo"]["type"];
$filesize = $_FILES["photo"]["size"];
// Verify file extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
// Verify file size - 5MB maximum
$maxsize = 5 * 1024 * 1024;
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
// Verify MYME type of the file
if(in_array($filetype, $allowed)){
// Check whether file exists before uploading it
if(file_exists("upload/" . $_FILES["photo"]["name"])){
echo $_FILES["photo"]["name"] . " is already exists.";
} else{
move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $_FILES["photo"]["name"]);
echo "Your file was uploaded successfully.";
}
} else{
echo "Error: There was a problem uploading your `enter code here`file. Please try again.";
}
} else{
echo "Error: " . $_FILES["photo"]["error"];
}
}
?>
</body>
</html>

Uploading photos in mysql and display using PHP

Hello guys, please help!
I'm having problems with my project, I followed these steps on how to upload(https://www.youtube.com/watch?v=Ipa9xAs_nTg) but something is wrong.
The error is Notice:
Undefined index: tmp_name in C:\xampp\htdocs\LDEVERACATERING\upload_process.php on line 16
Here is my upload.html
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.2.1.min"></script>
<script src="jquery-migrate-1.4.1.min"></script>
<title>
Image Upload
</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="content">
<form method="POST" action="upload_process.php" enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000"/>
<div>
<input type="file" name="image"/>
</div>
<div>
<textarea name="text" cols="40" rows="4" placeholder="Say something about this image..."></textarea>
</div>
<div>
<input type="submit" name="upload" value="Upload Image"/>
</div>
</form>
</div>
</body>
</html>
here is my upload_process.php
<?php
$msg = "";
//if upload button is pressed
if (isset($_POST['upload'])) {
//path to store the upload image
$target = "photos/".basename($_FILES['image']['name']);
//connect to database
$db = mysqli_connect("localhost", "root", "", "catering_info");
}
//get all the submitted date from the form
$image = $_FILES['image']['name'];
$text = $_POST['text'];
$sql = "INSERT INTO photos_upload(image, text) VALUES('$image', '$text')";
mysqli_query($db, $sql); //stores the submitted date into the database table: images
//now let's move the upload image into the folder: photos
if (move_uploaded_file($_FILES['tmp_name']['name'], $target)) {
$msg = "Image uploaded successfully";
}
else
{
$msg = "There was a problem uploading image";
}
?>
PLEASE HELP ME, I REALLY NEED IT. Also I'm still new to this, I just started web prog last 3 weeks :( Thank you very much!
Please 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'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== 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);
}
}
?>
Try this by replacing your code
move_uploaded_file($_FILES['tmp_name']['name'], $target)
With these
move_uploaded_file($_FILES['image']['tmp_name'], $target)

php not moving uploaded file on iis

iis_iusrs has read and write in the uploads dir "c:\temp" and in the destination folder "C:\inetpub\wwwroot\test\uploads".
After my script runs a tmp file is created. in the c:\temp dir however the file is not moved. When it runs on chrome it says "Success".
<?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'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 112097152) {
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp,"uploads/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action = "" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "image" />
<input type = "submit"/>
<ul>
<li>Sent file: <?php echo $_FILES['image']['name']; ?>
<li>File size: <?php echo $_FILES['image']['size']; ?>
<li>File type: <?php echo $_FILES['image']['type'] ?>
</ul>
</form>
</body>
</html>

How to upload a quakeml file using PHP?

I am trying to upload a quakeml file type format. It needs to be stored in a directory via a form but it does not upload anything. I have already written this code below.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<body>
<?php
if(isset($_FILES['file'])){
$file_name = $_FILES['file']['name'];
$file_size =$_FILES['file']['size'];
$file_tmp =$_FILES['file']['tmp_name'];
$file_type=$_FILES['file']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));
$expensions= array("quakeml");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a quakeml";
}
if($file_size > 4097152) {
$errors[]='File size must be excately 4 MB';
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp,"uploads/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="upload" />
</form>
</body>
</html>
Why is the above not uploading anything?

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>

Categories