Well this is my code but when I am trying to upload the doc file, the response is "Invalid file".....Thanks a lot. By the way my second question deals with rename of uploaded file to desired format "actual time + the original title" $date.
<?php
$datum = Date("j/m/Y/H/i/s", Time());
echo($date);
$allowedExts = array("doc");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "application/msword"))
&& ($_FILES["file"]["size"] < 2000000)
&& 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("uploaded_papers/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploaded_papers/" . $_FILES["file"]["name"]);
echo "Stored in: " . "uploaded_papers/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Change this:
$extension = end(explode(".", $_FILES["file"]["name"]));
To:
$extension = array_pop(explode(".", $_FILES["file"]["name"]));
Array_pop gives the last element from the array.
As to the proper MIME type for the Word document: http://filext.com/faq/office_mime_types.php
.doc
application/msword
.docx
application/vnd.openxmlformats-officedocument.wordprocessingml.document
So I would go for something like this:
$allowedMimes = array( "application/msword" , "application/vnd.openxmlformats-officedocument.wordprocessingml.document" , "application/vnd.ms-word");
if ($_FILES["file"]["size"] < 2000000
&& in_array($_FILES["file"]["type"], $allowedMimes))
To see the MIME type of the uploaded file, just echo $_FILES["file"]["type"]; and see that it's not an image. Please post back the mimetype so we can see what the "normal" doc is.
Updated the allowedMimes array with your own result.
Try this and see if this works for you
<?php
$datum = Date("j/m/Y/H/i/s", Time());
echo($date);
$allowedExts = array("doc","docx");
$extension = array_pop(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "application/msword"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "hello";
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("uploaded_papers/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
//changed file name
$file_name = time()."_".$_FILES['file']['name'];
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" .$file_name);
echo "Stored in: " . "uploaded_papers/" .$file_name;
}
}
}
else
{
echo "Invalid file";
}
?>
Changes
used array_pop instead of end.
changed the file name to be uploaded to currenttimestamp_orignalname.doc
why don't you do something like
$extension = explode(".", $_FILES["file"]["name"]);
if($extension[1]=='doc' || $extension[1]=='docx'){
///begin uploads
}else{
///fail message
}
Related
I have made a php code to upload a file....It uploads it sucessfully...but how do i phsicaly go and check where it is...?
NOTE : i am getting fail as output from the move_uploaded_file ()
here's my code :
<?php
$allowedExts = array("c", "cpp", "py", "java");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ( ($_FILES["file"]["size"] < 100000) && 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
{
mkdir ("./upload");
if (move_uploaded_file($_FILES["file"]["tmp_name"],"./upload/" . $_FILES["file"]["name"]))
{
echo "sucess";
}
else
{
echo "fail";
}
}
}
}
else
{
echo "Invalid file";
}
?>
you first check if move_uploaded_file actually able to move file or not as the directory you are moving must have write permissions otherwise move_uploaded_file fails
http://php.net/manual/en/function.move-uploaded-file.php
so must check like
if(move_uploaded_file($_FILES["file"]["tmp_name"],"./upload/" . $_FILES["file"]["name"])
//file moved
else
//error
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 a code and I would like to edit it so that the video uploaded shows up on my webpage and is then stored in the upload folder.
Code:
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
$allowType = array("video/mp4","audio/mp3","audio/wma","image/png","image/gif","image/jpeg");
$maxSize = 20000000000000;
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$pathToUpload = 'upload/';
if( in_array($_FILES["file"]["type"], $allowType) && in_array($extension, $allowedExts) && $_FILES["file"]["size"] <= $maxSize)
{
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($pathToUpload . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], $pathToUpload . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
I am not looking for someone to give me the code but to point me in the right direction.
At the point where the video is moved and you are echo'ing its location:
"Stored in: " . "upload/" . $_FILES["file"]["name"];
instead of echo'ing where it's stored you could add it to a videos table of some sort:
$videoLocation = "upload/".$_FILES['file']['name'];
// now insert $videoLocation into a database table
//so you can fetch it on whatever page you feel like
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);