I am trying to allow users to upload videos via this form
<form method='POST' name='uploadform' enctype='multipart/form-data' action=''>
Video: <input type='file' name='filename' /><br/>
<input type='submit' name='cmdSubmit' value='Upload' />";
Then with this php script I am trying to move the videos into another folder for storage.
if($_POST['cmdSubmit']){
$filename = basename($_FILES['filename']['name']);
move_uploaded_file($_FILES['filename']['tmp_name'], "videos/" . $filename);
}
The problem I am having is the video's are not being moved to my folder. I have done research on how to do this and have try many error solving methods but nothing is seeming to work so if you have any idea on what my problem is any help is appreciated. The form works perfectly. Thanks....
I have searched google and youtube and can't find any really good articles on this if you know any that would be awesome too.
I tried to do troubleshooting by using this code..
if(move_uploaded_file($_FILES['filename']['tmp_name'], "videos/" . $filename)){
echo "This worked Successfully";
}else{
echo "Error";
}
}
and it did not echo either statement....
Try the below PHP code
in this code you can have some Restrictions on Upload
you can set the Exts that you need to allow in upload..
HTML FORM CODE
<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>
PHP CODE BELOW
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_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";
}
?>
For Any Other Reference kindly Check Here http://w3schools.com/php/php_file_upload.asp Or Post A Comment
In php.ini Find and update as following:
post_max_size = 8M
upload_max_filesize = 2M
max_execution_time = 30
max_input_time = 60
memory_limit = 8M
Change to:
post_max_size = 750M
upload_max_filesize = 750M
max_execution_time = 5000
max_input_time = 5000
memory_limit = 1000M
Related
I'm currently trying to use the cURL executable to upload mp4 Files to a php script using the POST method. In the PHP file I'm checking the File format and all that kind of stuff. Here's the PHP file:
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if (($_FILES["file"]["type"] == "video/mp4") && ($_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("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";
}
?>
When I upload files using a normal HTML form it works properly. This is the HTML form i used:
<!DOCTYPE html>
<head>
<title></title>
</head>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data" name="uploadedfile">
<label for="file"><span>Filename:</span></label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
But when I now try it using the cURL Client using this command:
"curl -F file=#test.mp4 http://localhost:1337/upload_file.php"
It displays me "Invalid File" in the console, which is normally shown when the file doesnt match the attributes im checking in PHP(for example not fitting the file type).
I hope you guys understand my problem and can help me! :)
Greets Steven
Your $_FILES["file"]["type"] check is failing because cURL doesn't make any guesses to the type and won't send that automatically in the POST request.
This is easily spoofed so it's not a great check anyway.
But to make your example work, try specifying the content type with the file parameter:
curl -F file=#test.mp4;type=video/mp4 http://localhost:1337/upload_file.php
Further to what drew010 wrote (I'd write a comment but don't have the reputation yet), trusting those values passed in may actually be a security risk and has been used to hack sites in the past so be careful.
upload_file.php
$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma", "MP4");
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if ((($_FILES["file"]["type"] == "video/mp4")
|| ($_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"] < 20000000)
&& 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";
}
?>
html
<!DOCTYPE html>
<head>
<title></title>
</head>
<body>
<form action="upload_file.php" 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>
</body>
</html>
So my problem is I get the message "Invalid File". This only happens when I try to upload video type files. However when I try to upload an image it works like a charm. I've searched all over stackoverflow for other video file upload codes and still couldn't find any that worked. Anyone that could refer another question/solution to me and/or fix this problem will greatly be appreciated.
EXTRA NOTE
I've already tried adding echo "Its type is " . $_FILES["file"]["type"]; to debug what file type is being given however it just returns a nice white space.
Change this part
else
{
echo "Invalid file";
}
to
else
{
echo "Invalid file";
echo "Its type is " . $_FILES["file"]["type"];
}
Now upload the files that don't work and add those types to your list
Apparently the problem only was because the php.ini was set to only accept 10M the file I was uploading was over 15MB and so I guess it gave me the error. But shouldn't that give me the error file-size is too much or something? But that's basically the reason I got the error. :)
Im trying to get a form to upload mp3 or certain image files to a folder.
Using below code I can upload the image files ok, but when i try to upload an mp3 i get the error Undefined index: file in C:\wamp\www\recordlabel\inc\soundclips.php on line 6 and the code echos invalid file from my else block. can anyone offer assistance?
$allowedExts = array("mp3", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "audio/mp3")
|| ($_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"))
&& 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("../soundclips/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"../soundclips/" . $_FILES["file"]["name"]);
echo "Stored in: " . "../soundclips/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
And this is my form
<form class='form1' action='../inc/soundclips.php' method='post'
enctype='multipart/form-data'>
<input type="hidden" name="id" value="<?php echo $pid; ?>"/>
<b>Add Soundclip For <i><?php echo $e;?></i> </b>
<?php echo"<divclass='editimage'>";
echo "<img class='resizedimage' src='{$row['image']}' />";
echo"</div>";?><br />
<b>Song</b><br /><input type=text size='60' name='asong' /><br />
<input name='file' type="file" id="file" /><br />
<input type='submit' name='add' value='Add Soundclip' />
</form>
Looks like your mp3 file cannot be uploaded, so it is missing in $_FILES array. That might be due to its size compared to image files.
Please check upload_max_filesize and post_max_size settings from your php.ini and allow a greater size than your mp3 file.
I would so appreciate some help here please. I have been struggling without success for a full day to upload images to a shared host running Zeus (rather than apache--yes I know, they are changing!). The host blames the code yet wont tell me why "as they are not programmers" I have tried so many different version of the form script I am out of options. I have of course checked the upload limits on the php ini file configs (which I am not allowed to change by the host) and they are both 128meg. So it looks unlikely that is the cause. The outcome of the scripts is that we get to the final ''successfully loaded the file'' message but the files size loaded is zero (so there is nothing new in the target directory). I am relatively new to php so please do go easy on the jargon. Thank you.
Here are the two files>> First the form...
<form enctype="multipart/form-data" action="anewupload.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
and the php file refered to by the form...
<?php
$target = "uploads/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
//This is our size condition
if ($uploaded_size >350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
//This is our limit file type condition
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded to..";
echo $target;
echo "..The uploaded file size is $uploaded_size";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
First make sure the uploads folder exists and that is has write permissions set to either 755 or 777
Plus, you have two unassigned variables, $uploaded_size and $uploaded_type, so that will fail.
You would need to use something like if ($_FILES["file"]["size"] <350000) etc. probably another reason why it's failing.
Give this a try, see if this works for you, it's what I use:
Modify $allowedExts = array("gif", "jpeg", "jpg", "png"); for permitted file extensions.
It will upload only if size is less than 350000
NOTE: Change <input name="uploaded" type="file" /> to <input name="file" type="file" /> see form under handler 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"] < 350000)
&& 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("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";
}
?>
Also change your form to this to reflect the PHP handler:
<form enctype="multipart/form-data" action="anewupload.php" method="POST">
Please choose a file: <input name="file" type="file" /><br />
<input type="submit" value="Upload" />
</form>
I have two simple starter scripts (html and php) to choose a file from the machine and then upload to server. However when I press upload, it appears to be uploading the actual php script that deals with the upload. here they are:
<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>
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_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";
}
?>
THis i'd imagine is just a silly mistake somewhere on my part, but this is my first experience in uploading a file using php. thanks very much.