I am uploading jpg files but this code is giving me error, What i am doing wrong.
If I remove this code from my script, it works perfectly, but i want to restrice user to upload only jpg/gif and png files
I also tried by only giving jpg condition, but not working.
//This is our limit file type condition
$allowfiles = array("image/jpg", "image/gif", "image/jpeg", "image/png");
if (!in_array($uploaded_type, $allowfiles)) {
$uploadingerr = $uploadingerr . "Only JPG, GIF and PNG files allowed <br/>";
$ok=0;
}
$uploaded_type doesn’t seem to be defined anywhere in the code you posted.
Try placing the following code on the line after where you define $allowtypes:
var_dump($uploaded_type); die();
It’ll dump the value of $uploaded_type to the browser.
Are you sure that variable $uploaded_type is filled in correctly? Also, are you sure that image types you sent actually correspond to the ones specified in the array that allows the types?
Without seeing the rest of the code, it’s impossible to see what you’re doing wrong.
Complete Code:
<input name="uploaded" type="file" /><br />
$folder = "uploads/";
$f1 = $folder . date("dmyis") . basename( $_FILES['uploaded']['name']) ;
$ok=1;
//This is our size condition
if ($uploaded_size > 550000)
{
$uploadingerr = "Your file is too large.<br>";
$ok=0;
}
//This is our limit file type condition
$allowfiles = array('image/jpg', 'image/gif', 'image/jpeg', 'image/png');
if (!in_array($uploaded_type, $allowfiles)) {
$uploadingerr = $uploadingerr . "Only JPG, GIF and PNG files allowed <br/>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
$uploadingerr = $uploadingerr . "Sorry your file was not uploaded <br/>";
}
//If everything is ok we try to upload it
else
{
$er = "Sorry, there was a problem uploading your file.";
if($_FILES['uploaded']['name']) {
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $f1))
{
//echo "". basename( $_FILES['uploadedfile']['name']). " has been uploaded<br/>";
$pf1=1;
}
}
}
Reference:
furicane:
Are you sure that variable $uploaded_type is filled in correctly? Also, are you sure that image types you sent actually correspond to the ones specified in the array that allows the types?
Without seeing the rest of the code, it’s impossible to see what you’re doing wrong.
[URL=“http://php.about.com/od/advancedphp/ss/php_file_upload.htm ”]
Steve_
January 3, 2011, 5:48pm
6
Seems the copy paste they have done at the about.com site is not very good…
There other stuff missing too.
$uploaded_size = $_FILES[‘uploaded’][‘name’];
$uploaded_type= $_FILES[‘uploaded’][‘type’];
To do a real check for images, at least for jpeg use this:
if (@exif_imagetype ($img[‘tmp_name’]) != IMAGETYPE_JPEG) return false;
Check manual for the exif function
This code is also not working
Steve_:
Seems the copy paste they have done at the about.com site is not very good…
There other stuff missing too.
$uploaded_size = $_FILES[‘uploaded’][‘name’];
$uploaded_type= $_FILES[‘uploaded’][‘type’];
To do a real check for images, at least for jpeg use this:
if (@exif_imagetype ($img[‘tmp_name’]) != IMAGETYPE_JPEG) return false;
Check manual for the exif function
Ernie1
January 4, 2011, 7:47pm
8
<?php
if (isset($_POST['submit']))
{
$uploaded_size = $_FILES['uploaded']['size'];
$uploaded_type = $_FILES['uploaded']['type'];
$uploadingerr = "";
$folder = "uploads/";
$f1 = $folder . date("dmyis") . basename($_FILES['uploaded']['name']);
$ok = 1;
if ($uploaded_size > 550000)
{
$uploadingerr = "Your file is too large.<br>";
$ok = 0;
}
$allowfiles = array(
'image/jpg',
'image/gif',
'image/jpeg',
'image/png'
);
if (!in_array($uploaded_type, $allowfiles))
{
$uploadingerr = $uploadingerr . "Only JPG, GIF and PNG files allowed <br/>";
$ok = 0;
}
if ($ok == 0)
{
$uploadingerr = $uploadingerr . "Sorry your file was not uploaded <br/>";
}
else
{
$er = "Sorry, there was a problem uploading your file.";
if ($_FILES['uploaded']['name'])
{
if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $f1))
{
echo "" . basename($_FILES['uploaded']['name']) . " has been uploaded<br/>";
$pf1 = 1;
}
}
}
}
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" name="submit" value="Upload" />
</form>
Thanks for this code, works perfectly.
Now, If I decided to use 3 file uploads, and want to use function in order to reuse the validation code.
I change the code but not working…Sorry, I was asking for help again and again.
if (isset($_POST['submit']))
{
$uploaded_size = $_FILES['uploaded']['size'];
$uploaded2_size = $_FILES['uploaded2']['size'];
$uploaded3_size = $_FILES['uploaded3']['size'];
$uploaded_type = $_FILES['uploaded']['type'];
$uploaded2_type = $_FILES['uploaded2']['type'];
$uploaded3_type = $_FILES['uploaded3']['type'];
$uploadingerr = "";
$folder = "uploads/";
$f1 = $folder . date("dmyis") . basename($_FILES['uploaded']['name']);
$ok = 1;
$allowfiles = array(
'image/jpg',
'image/gif',
'image/jpeg',
'image/png'
);
function chksize($n) {
if ($n > 550000)
{
$uploadingerr = "Your file is too large.<br>";
$ok = 0;
}
}
function chktype($n) {
if (!in_array($n, $allowfiles))
{
$uploadingerr = $uploadingerr . "Only JPG, GIF and PNG files allowed <br/>";
$ok = 0;
}
}
chksize($uploaded_size);
chksize($uploaded2_size);
chksize($uploaded3_size);
chktype($uploaded_type);
chktype($uploaded2_type);
chktype($uploaded3_type);
if ($ok == 0)
{
$uploadingerr = $uploadingerr . "Sorry your file was not uploaded <br/>";
}
else
{
$er = "Sorry, there was a problem uploading your file.";
if ($_FILES['uploaded']['name'])
{
if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $f1))
{
echo "" . basename($_FILES['uploaded']['name']) . " has been uploaded<br/>";
$pf1 = 1;
}
}
}
}
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" name="submit" value="Upload" />
</form>
Ernie1:
<?php
if (isset($_POST['submit']))
{
$uploaded_size = $_FILES['uploaded']['size'];
$uploaded_type = $_FILES['uploaded']['type'];
$uploadingerr = "";
$folder = "uploads/";
$f1 = $folder . date("dmyis") . basename($_FILES['uploaded']['name']);
$ok = 1;
if ($uploaded_size > 550000)
{
$uploadingerr = "Your file is too large.<br>";
$ok = 0;
}
$allowfiles = array(
'image/jpg',
'image/gif',
'image/jpeg',
'image/png'
);
if (!in_array($uploaded_type, $allowfiles))
{
$uploadingerr = $uploadingerr . "Only JPG, GIF and PNG files allowed <br/>";
$ok = 0;
}
if ($ok == 0)
{
$uploadingerr = $uploadingerr . "Sorry your file was not uploaded <br/>";
}
else
{
$er = "Sorry, there was a problem uploading your file.";
if ($_FILES['uploaded']['name'])
{
if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $f1))
{
echo "" . basename($_FILES['uploaded']['name']) . " has been uploaded<br/>";
$pf1 = 1;
}
}
}
}
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" name="submit" value="Upload" />
</form>
Ernie1
January 7, 2011, 6:19pm
10
Try this:
<?php
ini_set("display_errors", 1);
error_reporting(-1);
if (isset($_POST['submit']))
{
foreach ($_FILES['uploaded']['error'] as $key => $value)
{
if ($value == UPLOAD_ERR_OK)
{
$uploaded_size = $_FILES['uploaded']['size'][$key];
$uploaded_type = $_FILES['uploaded']['type'][$key];
$uploadingerr = "";
$folder = "uploads/";
$f1 = $folder . date("dmyis") . basename($_FILES['uploaded']['name'][$key]);
$ok = 1;
if ($uploaded_size > 550000)
{
$uploadingerr = "Your file " . $_FILES['uploaded']['name'][$key] . " is too large.<br />";
$ok = 0;
}
$allowfiles = array(
'image/jpg',
'image/gif',
'image/jpeg',
'image/png'
);
if (!in_array($uploaded_type, $allowfiles))
{
$uploadingerr .= "Only JPG, GIF and PNG files allowed <br />";
$ok = 0;
}
if ($ok == 0)
{
echo $uploadingerr;
}
else
{
if (move_uploaded_file($_FILES['uploaded']['tmp_name'][$key], $f1))
{
echo basename($_FILES['uploaded']['name'][$key]) . " has been uploaded<br />";
}
}
}
}
}
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
Please choose a file 1: <input name="uploaded[]" type="file" /><br />
Please choose a file 2: <input name="uploaded[]" type="file" /><br />
Please choose a file 3: <input name="uploaded[]" type="file" /><br />
<input type="submit" name="submit" value="Upload" />
</form>
Thanks, this was a good lesson for me. One more question, If we want to use class or functions without using loops. how can we do this? This is just for learning purpose.
Ernie1:
Try this:
<?php
ini_set("display_errors", 1);
error_reporting(-1);
if (isset($_POST['submit']))
{
foreach ($_FILES['uploaded']['error'] as $key => $value)
{
if ($value == UPLOAD_ERR_OK)
{
$uploaded_size = $_FILES['uploaded']['size'][$key];
$uploaded_type = $_FILES['uploaded']['type'][$key];
$uploadingerr = "";
$folder = "uploads/";
$f1 = $folder . date("dmyis") . basename($_FILES['uploaded']['name'][$key]);
$ok = 1;
if ($uploaded_size > 550000)
{
$uploadingerr = "Your file " . $_FILES['uploaded']['name'][$key] . " is too large.<br />";
$ok = 0;
}
$allowfiles = array(
'image/jpg',
'image/gif',
'image/jpeg',
'image/png'
);
if (!in_array($uploaded_type, $allowfiles))
{
$uploadingerr .= "Only JPG, GIF and PNG files allowed <br />";
$ok = 0;
}
if ($ok == 0)
{
echo $uploadingerr;
}
else
{
if (move_uploaded_file($_FILES['uploaded']['tmp_name'][$key], $f1))
{
echo basename($_FILES['uploaded']['name'][$key]) . " has been uploaded<br />";
}
}
}
}
}
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
Please choose a file 1: <input name="uploaded[]" type="file" /><br />
Please choose a file 2: <input name="uploaded[]" type="file" /><br />
Please choose a file 3: <input name="uploaded[]" type="file" /><br />
<input type="submit" name="submit" value="Upload" />
</form>