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
Related
I am having an issue with undefined error in my php. After research I know I know I should use isset but I'm not sure how to implement it. Here is the peice of code that causes the issue:
if (($_FILES["file"]["type"] == "video/mp4") && ($_FILES["file"]["size"] < 20000))
{
Should I put the isset inside the if function before each $_FILES or where should it go ?
Here is the full piece of code, as you will probably notice I am quite new to PHP:
$ivalid_file='0';
if (($_FILES["file"]["type"] == "video/mp4") && ($_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("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";
}
As far as I understand, you want to know how to check if the file was ever uploaded and how to implement it in code. All you gotta do is, wrap you entire code in an if block which checks if $_FILES['files'] is set like so:
//Check if "file" exists here:
if(isset($_FILES["file"])) {
$ivalid_file='0';
if (($_FILES["file"]["type"] == "video/mp4") && ($_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("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";
}
} else {
// Display appropriate error
}
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
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
}
I am trying to combined these two script (a file upload) and (a mysql update) so that the image file is both uploaded to the correct folder and the file path is then updated in the mysql database. I know the $sql update query is wrong and thats where my trouble is. Any help would be great.
//db connection
require "connect.db.php";
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_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("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";
}
// update data in mysql database
$sql="UPDATE `characters` SET ch_image='/upload/$_FILES["file"]["name"]' WHERE ID='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='test.html'>View result</a>";
touch('../file.html');
clearstatcache();
}
else {
echo "Whoops: " . mysql_error(); ;
}
mysql_close();
?>
change $sql to this
$sql="UPDATE `characters` SET ch_image='/upload/" . $_FILES['file']['name'] . "' WHERE ID='$id'";
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);