I keep getting the invalid file error. Can anyone see what is wrong with this script please. I got it from w3 schools and the folder "pics/2012/Blackhall Primary/" does exist
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
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"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("pics/2012/Blackhall Primary/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"pics/2012/Blackhall Primary/" . $_FILES["file"]["name"]);
echo "Stored in: " . "pics/2012/Blackhall Primary/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
include more error checking like this:
<?php
echo process_files();
function process_files()
{
$allowed_file_types=array('image/gif','image/jpeg','image/png','image/jpg','image/pjpeg');
if(0==sizeof($_FILES)) return 'no files uploaded';
if(!stristr($_FILES['file']['type'], 'image')) return 'file is not an image';
if(!in_array($_FILES['file']['type'], $allowed_file_types)) return 'image type '.$_FILES['file']['type'].' is not allowed';
if($_FILES['file']['size'] < 20000) return 'file size too large. Max:20000, File:'.$_FILES['file']['size'];
// rest of your code here!
}
?>
check the extension of your pics, maybe are in uppercase
Related
I have code to upload an image onto my website and I just want to know how to make it so I can set it to 'default.png' if no file is chosen by the user.
This is the code extract from the form
<label for="file">Blog Post Image:</label>
<input type="file" name="file" id="file">
and this is the PHP code that uploads the file
$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"] < 40000)
&& 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"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("images/blog/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
"../images/blog/" . $_FILES["file"]["name"]);
echo "Stored in: " . "../images/blog/" . $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file";
}
and then after this is just gets added to a database...
Keep default.png within images/blob/. Check if upload is left empty with is_uploaded_file().
if(!file_exists($_FILES['file']['tmp_name']) || !is_uploaded_file($_FILES['file']['tmp_name'])) {
echo 'No upload';
// use images/blog/default.png
echo "Upload: default.png <br>";
echo "Type: image/png <br>";
echo "Size: " . filesize("images/blog/default.png") . " bytes<br>";
echo "Temp file: images/blog/default.png <br>";
} else{
$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"] < 40000)
&& 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"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("images/blog/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
"../images/blog/" . $_FILES["file"]["name"]);
echo "Stored in: " . "../images/blog/" . $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file";
}
}
i am having an issue executing an SQL/SQLi query and uploading an image at the same time.
I am able to do one or the other but not sure how to combine the code.
The main code:
<?php
require('connect.php');
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"];
}
$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 "Return Code: " . $_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 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/" . $_FILES["file"]["name"]);
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
The above code works for image upload
<?php
$venuename=$_POST['venuename'];
$adress=$_POST['adress'];
$venuetype=$_POST['venuetype'];
$venuedesc=$_POST['description'];
if($venuename&&$adress&&$venuetype&&$venuedesc)
{
$query = mysql_query("INSERT INTO tbl_venues (venue_name,venue_description,venue_adress,venue_type) VALUES ('$venuename','$venuedesc','$adress','$venuetype')");
header("Location: created_venue.php?");
}
else
{
echo '<div id="venuevalidation">Please complete all fields!</div>';
}
?>
The above works for inserting data however my attempts to combine the two are unsuccessful, any suggestions?
Apologies for the large post! Thanks
All resolved, the issue was with brackets! the code was executing on one part and not moving to the next.
I have been uploading a file for last few hours. But always stuck at this point.
here is my controller function for upload:
function upload()
{
echo $targetPath = site_url('/uploads');
echo "<br />";
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
echo $_FILES["file"]["type"];
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")))
{
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"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists($targetPath.'/'.$_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
if(move_uploaded_file($_FILES["file"]["tmp_name"],$targetPath.'/'. $_FILES["file"]["name"]))
{
echo "Stored in: " .$targetPath.'/' . $_FILES["file"]["name"];
}
else
{
echo "not uploaded";
}
}
}
and here is the output :
http://localhost/sites/public_html/site/uploads
image/jpegUpload: 1468716_668135866543165_1899878158_n.jpg
Type: image/jpeg
Size: 57.09375 kB
Temp file: C:\wamp\tmp\phpC390.tmp
not uploaded
Everything is going fine.
But its just not UPLOADING at desired location. What could be wrong ?
You cannot move_uploaded_file to an HTTP destination; it must be a local filesystem path.
Your $targetPath must be something like c:\wamp\www\sites\public_html\... instead.
i have converted an image to and froth base64 string,but now i would like to upload an image and then convert it into a base64string,and i would like to send this string as an input to call the web service.
the code i am using is
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& 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"] / 1024) . " 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";
}
?>
try this:
$image= file_get_contents("/path/to/image.jpg");
$convertedData= base64_encode($imagedata);
edit
It seems you do not know basic php. should i help you?
I am echoing base64 formated code
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& 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"] / 1024) . " 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"].'<br />';
/* Notice here */
$image = file_get_contents("upload/" . $_FILES["file"]["name"]);
$convertedData = base64_encode($imagedata);
echo 'Base64 Inmage code :'.$convertedData;
/* My edits end here */
}
}
}
else
{
echo "Invalid file";
}
?>
I'm trying to rename an image when uploading it to my server. Is there generally an easy way of doing this? Below is the php code I'm using. I want to rename it as a variable I'm passing through as a hidden field from a html form.
//variable from hidden field on form which is from mysql database
$imageName = $_POST['image_rename'];
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 8000000))
{
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"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("../uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "../uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "../uploads/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
Try changing the move_uploaded_file() function; add the name you want within the function. Just make sure to grab the proper extension, something like:
$parts=explode('.',$_FILES['file']['name']);
$newName=$imageName.'.'.$parts[(count($parts)-1)];
move_uploaded_file($_FILES['file']['tmp_name'],'../uploads/'.$newName);
Change
move_uploaded_file($_FILES["file"]["tmp_name"], "../uploads/" . $_FILES["file"]["name"]);
to
move_uploaded_file($_FILES["file"]["tmp_name"], "../uploads/" . $imageName);