PHP File Upload Does Nothing - php

I've had several upload forms working before, however, even after almost copying my previous code this on doesn't seem to work, I prefer doing it all in one php script file and so it is all generated in this single file.
My form:
<form action="" method="post" enctype="multipart/form-data">
<ul>
<li>
<label for="file">File : </label>
<input type="file" id="file" name="file" required="required" />
</li>
<li>
<input type="submit" value="Upload" />
</li>
</ul>
</form>
My php upload:
if(!empty($_POST['file']))
{
echo "Found.";
$exts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$ext = end($temp);
if((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($ext, $exts))
{
if($_FILES["file"]["error"] > 0)
{
$result = "Error Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
$scandir = scandir("/images/news/");
$newname = (count($scandir-2)) . $ext;
move_uploaded_file($_FILES["file"]["tmp_name"],"/images/news/" . $newname);
$ulink = "/images/news/" . $newname;
$result = "Success, please copy your link below";
}
}
else
{
$result = "Error.";
}
}
When I upload a .png image, the page simply seems to refresh, I've placed the echo "Found."; in there to check if it even has anything in $_POST["file"] but it doesn't seem to have anything.
I don't understand why the page isn't submitting correctly. I've changed action="" to action="upload.php" to make sure it points to the same page but still nothing.

Use $_FILES['file'] instead of $_POST['file'].
Read more about $_FILES at http://www.php.net/manual/en/features.file-upload.post-method.php

replace $_POST['file'] by $_FILES['file'] and set action="".

Try this.... because $_POST not work with files, for files we use $_FILES..
if(!empty($_FILES['file']))
{
echo "Found.";
$exts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$ext = end($temp);
if((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($ext, $exts))
{
if($_FILES["file"]["error"] > 0)
{
$result = "Error Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
$scandir = scandir("/images/news/");
$newname = (count($scandir-2)) . $ext;
move_uploaded_file($_FILES["file"]["tmp_name"],"/images/news/" . $newname);
$ulink = "/images/news/" . $newname;
$result = "Success, please copy your link below";
}
}
else
{
$result = "Error.";
}
}

I wouldn't just check the $_FILES variable. I would name the submit input and check if the submit input was submitted. This way you can check if the button was pressed with no files selected and prompt the user as such.
Like So:
<form action="" method="post" enctype="multipart/form-data">
<ul>
<li>
<label for="file">File : </label>
<input type="file" id="file" name="file" required="required" />
</li>
<li>
<input type="submit" value="Upload" name="upload"/>
</li>
</ul>
</form>
Then you can check the post variable for that value.
Like So:
if(!empty($_POST['upload']))
{
echo "Found.";
$exts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$ext = end($temp);
if((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($ext, $exts))
{
if($_FILES["file"]["error"] > 0)
{
$result = "Error Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
$scandir = scandir("/images/news/");
$newname = (count($scandir-2)) . $ext;
move_uploaded_file($_FILES["file"]["tmp_name"],"/images/news/" . $newname);
$ulink = "/images/news/" . $newname;
$result = "Success, please copy your link below";
}
}
else
{
$result = "Error.";
}
}

Related

Notice: Undefined index: file uploading video

I am trying to upload a video to my uploads folder. I got the code from another question on here and that works fine. But I keep getting this notice error and I don't know how to fix it. I've been trying all day. I tried to check if it was isset() and that still didn't work. Can someone help me please ?
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
$_FILES = $_FILES['file'];
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$unique = date('Y-m-d_H-i-s');
if ((null !==($_FILES["file"]["type"] == "video/mp4")
|| (null !==($_FILES["file"]["type"] == "audio/mp3"))
|| ($_FILES["file"]["type"] == "audio/wma")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
echo 'File uploaded successfully';
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
$datetime = date('Y-m-d_H-i-s');
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"] . $datetime . md5($_FILES["file"]["name"]));
}
}
} else {
echo "Invalid file";
}
?>
<form action="profile.php" id="videoupload" method="post" enctype="multipart/form-data">
<label for="file"><span>Filename:</span></label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
You're trying to process the file upload before a file is uploaded. You have to check if the form was posted first.
<?php
if (isset($_FILES['file'])) {
$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
// $_FILES = $_FILES['file']; // <-- remove this line
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$unique = date('Y-m-d_H-i-s');
if ((null !==($_FILES["file"]["type"] == "video/mp4")
|| (null !==($_FILES["file"]["type"] == "audio/mp3"))
|| ($_FILES["file"]["type"] == "audio/wma")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
echo 'File uploaded successfully';
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
$datetime = date('Y-m-d_H-i-s');
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"] . $datetime . md5($_FILES["file"]["name"]));
}
}
} else {
echo "Invalid file";
}
}
?>
<form action="profile.php" id="videoupload" method="post" enctype="multipart/form-data">
<label for="file"><span>Filename:</span></label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
$_FILES['file']
means you have input type = file named 'file'. if you don't have the input named 'file', you got an undefined index notice.
if you have that,
$_FILES = $_FILES['file'];
this will bring error to other codes. because it try to override $_FILES.

File upload script returning false

I am trying to make a script that allows users to change their profile picture for a website that I am making. Here is the HTML code for the form:
<form action="upload_prof_pic.php" method="POST" enctype="multipart/form-data">
<input type="file" class="btn btn-default" name="file" id="file" /><br /><br />
<input type="submit" class="btn btn-default" value="Upload Profile Picture" />
</form>
Here is the PHP code for upload_prof_pic.php:
<?php
session_start();
require("includes/connect.php");
$results = $db->query("SELECT * FROM users WHERE username='".$_SESSION["logged_in"]."'");
$rows = $results->fetch();
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if (
(
($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
)
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts)
) {
if ($_FILES["file"]["error"] > 0) {
//error: uploading
header("Location: account.php?err=upload");
} else {
//success
move_uploaded_file($_FILES["file"]["tmp_name"],
"prof_pic/users/".$rows["username"]."/" . $_FILES["file"]["name"]);
$db->query("UPDATE users SET prof_pic='".$_FILES['file']['name']."' WHERE username='".$_SESSION["logged_in"]."'");
header("Location: account.php");
}
} else {
//error: invalid file
header("Location: account.php?err=invalid");
}
?>
It always runs the '//error: invalid file' part of the script. Can anyone help? It worked once, then I changed something in the '//success' part. This shouldn't have had any effect, but apparently it did.
i got that same error so i used following javascript coding.
<script type="text/javascript" src="js/jquery-func.js"></script>
<SCRIPT type="text/javascript">
function ValidateFileUpload() {
var fuData = document.getElementById('filename');
var FileUploadPath = fuData.value;
//To check if user upload any file
if (FileUploadPath == '') {
alert("Please upload an image");
} else {
var Extension = FileUploadPath.substring(
FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
//The file uploaded is an image
if (Extension == "gif" || Extension == "png" || Extension == "bmp"
|| Extension == "jpeg" || Extension == "jpg")
{
<?php
move_uploaded_file($_FILES["file"]["tmp_name"],
"prof_pic/users/".$rows["username"]."/" . $_FILES["file"]["name"]);
$db->query("UPDATE users SET prof_pic='".$_FILES['file']['name']."' WHERE username='".$_SESSION["logged_in"]."'");
header("Location: account.php");
?>
}
//The file upload is NOT an image
else {
alert("Photo only allows file types of GIF, PNG, JPG, JPEG and BMP. ");
}
}
}
html part
<input onchange="ValidateFileUpload(this);" type="file" name="file" id="filename"/>

PHP file uploader (images) Not uploading certain images

Trying to upload certain images with PHP but it won't work
I have used the example from w3Schools to try and upload an image with a higher value for hight then width.
It's working fine on "normal" images (horizontal), but as I said I'm not getting it to work on vertical aligned images.
The script does not recognise the file at all I think.
The errormessage is : Undefined index on the line where this goes $_FILES["file"]["name"])
****THIS SCRIPT IS JUST FOR CHECKING THE FILE I KNOW IT DOESENT UPLOAD IT****
here is the image im trying to get PHP to check http://rubenringdal.net/img/testigjen.jpg
<html>
<head>
<title></title>
</head>
<body>
<form action="uploader.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
else
{
echo "Invalid file";
}
?>
If your image is large than what you are allowing, 20 kb, it will not work so change this to a greater value.
($_FILES["file"]["size"] < **20000**)
And you have to check if anything has been uploaded before you process it.
<?php
if(!empty($_FILES["file"]))//Check if its not empty
{
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
else
{
echo "Invalid file";
}
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form action="uploader.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

Upload Image on server using php but it doesn't exsist on server

I am really confused I have this image upload code and it's working fine on my home server "Xampp" and when I click on upload button it upload image and send it to Upload folder but When I upload this php and html page to server and works fine but it can't save image to Upload folder on server please help me out. Thanks
you can try it on my site
http://bing.freevar.com/image_upload.html
Here is HTML file
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Here is a PHP file
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] <= 200000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 10024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
chmod("upload", 0644);
?>
If it works on your local server, but not on production you need to make sure that the folder exists on the production server and that it can be written to by the user account that the PHP script is executing under.
I think you got this PHP script from this link of w3schools.com. This php script has two extra parentheses in if condition. Remove these extra parentheses.
if (/*removed*/($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/x-png")// removed
&& ($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts))
It worked for me. Hope it will work for you also. I think you need more validation. You can use getimagesize() function to check width, height, MIME type, attr of the uploaded image.

Multiple Photo Upload PHP

I've looked around and have struggled to find out how to upload multiple images (.jpg/.png/etc).
For my task I was looking to upload 5 pictures to the database record. So far I'm struggling to even upload 5 together in one record. I've used PHP from the Ws3 website and it works successfully but this code is for one image alone.
PHP Code -
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . uniqid() . "_" . $_FILES["file"]["name"]);
}
}
}
else
{
$error = "Invalid file";
}
My HTML is as follows,
<label for="file">Filename:</label>
<input type="file" name="file" id="file">
<input type="file" name="file" id="file">
<input type="file" name="file" id="file">
<input type="file" name="file" id="file">
<input type="file" name="file" id="file">
Any advice is greatly appreciated guys! Cheers
First, add enctype="multipart/form-data" to your form. Then change the names of the file fields to:
<input type="file" name="file[]" id="file" >
<input type="file" name="file[]" id="file" >
<input type="file" name="file[]" id="file" >
This will create an array of the files submitted. Now you'll be able to do a foreach on the files. Quick example:
foreach ($_FILES['file']['name'] as $f => $name) {
}
Around your existing code, then add [$f] to every $_FILES["file"] variable. So $_FILES["file"]["size"] has to be changed to $_FILES["file"]["size"][$f] and so on. In the foreach I referring $name to $_FILES["file"]["name"][$f], so you can use $name instead.
Full php code based on your script:
foreach ($_FILES['file']['name'] as $f => $name) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $name);
$extension = end($temp);
if ((($_FILES["file"]["type"][$f] == "image/gif")
|| ($_FILES["file"]["type"][$f] == "image/jpeg")
|| ($_FILES["file"]["type"][$f] == "image/jpg")
|| ($_FILES["file"]["type"][$f] == "image/png"))
&& ($_FILES["file"]["size"][$f] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"][$f] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"][$f] . "<br>";
}
else
{
if (file_exists("upload/" . $name))
{
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"][$f], "upload/" . uniqid() . "_" . $name);
}
}
}
else
{
$error = "Invalid file";
}
}
At the end, I would suggest you to go learn more about PHP on different sites. W3schools is easy, but also not your best option. As an example, you are checking this:
if ((($_FILES["file"]["type"][$f] == "image/gif")
|| ($_FILES["file"]["type"][$f] == "image/jpeg")
|| ($_FILES["file"]["type"][$f] == "image/jpg")
|| ($_FILES["file"]["type"][$f] == "image/png"))
&& ($_FILES["file"]["size"][$f] < 2000000)
&& in_array($extension, $allowedExts))
But only the array will be enough, like this:
if ($_FILES["file"]["size"][$f] < 2000000 && in_array($extension, $allowedExts))
$_FILES["file"]["type"] not always returns the type as expected.. We see it many times in audio and video files. Another post on SO about MIME types referring to the W3schools script: PHP: $_FILES["file"]["type"] is useless
Before asking new questions on StackOverflow, please do a search. I just wrote a long answer just for you, but the same thing has been done MANY times for this kind of question already. https://stackoverflow.com/search?q=multiple+upload+php
Hope it answers your question, and good luck learning PHP.
I think the problem is in your HTML... instead of five of these:
<input type="file" name="file" id="file">
Try doing it like this:
<input type="file" name="file[]">
That will submit an array of files, rather than trying to submit multiple files under the same input name.
As a side note, you shouldn't use the same id attribute on more than one element. Perhaps you meant to use a class?
if (isset($_POST["kirim"])) {
$jumlah = count($_FILES['gambar']['size']); // cari jml gambar
$size = $_FILES['gambar']['size']; //cek ukuran file
$ext = $_FILES['gambar']['type']; // cek extensi file
$max_size = 1000000;
$htg = implode(" ",$size);
if ($htg < 1){
echo "<script>alert('pilih file yang akan di upload !');
location.href='index.php';</script>";
}
elseif(max($size) > $max_size){
echo "<script>alert('ukuran file terlalu besar');
location.href='index.php';</script>";
}elseif (in_array("application/octet-stream", $ext) || in_array("application/pdf",$ext) || in_array("text/plain", $ext)) {
echo "<script>alert('file harus jpg atau png semua !');
location.href='index.php';</script>";
}else{
for ($i=0; $i < $jumlah; $i++) {
$file_name = $_FILES['gambar']['name'][$i];
$tmp_name = $_FILES['gambar']['tmp_name'][$i];
move_uploaded_file($tmp_name, "uploads/".$file_name);
mysqli_query($conn,"INSERT INTO images VALUES
('','$file_name','$tmp_name')");
}
echo "<script>alert('Upload Berhasil');location.href='index.php';</script>";
}
}

Categories