I want to upload video files in php for that i am using following code
PHP
$newUploadDir = "c://video";
$idx = "file";
if (isset($_FILES[$idx]) && is_array($_FILES[$idx])) {
echo "file set";
foreach ($_FILES[$idx]["error"] as $key => $error) {
echo "loop";
if ($error == UPLOAD_ERR_OK) {
echo "<br/>dd2";
$tmp_name = $_FILES[$idx]["tmp_name"][$key];
$name = $_FILES[$idx]["name"][$key];
$ext1 = explode(".", $name);
$extension = end($ext1);
$newfilename = "test".".".$extension;
$video_types = array('mp4', 'avi','webm');
if (in_array($extension, $video_types)) {
if (move_uploaded_file($tmp_name, $newUploadDir.$newfilename)) {
echo "uploaded to folder";
} else {
echo "Not uploaded to folder";
}
}
} else {
echo "not uploaded $error";
}
}
}
echo "ok";
HTML
<form action="http://localhost/fileupload/video.php" enctype="multipart/form-data" method="post">
<input id="file1" name="file[]" type="file"/>
<input name="userId" type="text" value="2"/>
<input id="Submit" name="submit" type="submit" value="Submit" />
</form>
Output
file setloopnot uploaded 1ok
Video file is not uploading. How to resolve this?
Actually, if you try to upload very large (video) files, probably the upload file size limit will not let you do that. Instead of regular file upload, there are other possibities. For the begining, look at this, this or this.
An other aproach would be to use third party services like Amazon S3, Microsoft Azure, etc.
Related
Hi I'm new to php and keep getting this error: "Warning: Invalid argument supplied for foreach() in /home/site/folder/upload.php on line 61."
I'm trying to build a form in which users can upload one or more photos automatically to a directory to then be displayed else where.
Whenever I use this form I created it functions properly on my website but unfortunately it keeps printing that error out and would like it to go away. Here is my code I'm working with:
<div>
<form action="upload.php" enctype="multipart/form-data" method="POST">
<input type="file" name="images[]" multiple="multiple"/>
<input type="submit" name="submit" value="upload images"/>
<form/>
<?php
// check if uploads directory exists
$dir = "images/";
if(!is_dir($dir))
{
echo "Directory not found, let's create the folder.";
mkdir($dir,"0777", true);
}
$countimg = 0;
$allimg = 0;
foreach($_FILES["images"]["name"] as $k=>$name)
{
$allimg++;
$imgname = $_FILES["images"]["name"][$k];
$sizeimg = $_FILES["images"]["size"][$k];
$tmpname = $_FILES["images"]["tmp_name"][$k];
//2.
$extension = strtolower(pathinfo($dir.$imgname, PATHINFO_EXTENSION));
if($extension=='png' || $extension=='jpg' ||$extension=='jpeg' ||$extension=='gif')
{
if($sizeimg < 2097152){
if(!file_exists($dir.$imgname)){
//1.
if(move_uploaded_file($tmpname,$dir.$imgname))
{
$countimg++;
}
}
}
}
}
echo "You are trying to upload $allimg images".'<br>';
echo "From $allimg image(s) - $countimg was/were uploaded with success".'<br>';
$z = $allimg - $countimg;
echo "$z image(s) were not uploaded: Not an image, over 2MB, or already uploaded.";
?>
</div>
Try
if (count($_FILES)) {
foreach($_FILES["images"]["name"] as $k=>$name) {
....
}
}
I tested your script, it works fine. The error message appears because you are not checking that a file got uploaded before starting the foreach. If I land on the page, the PHP code will still be triggered. To fix this, you may use the below:
<div>
<form action="upload.php" enctype="multipart/form-data" method="POST">
<input type="file" name="images[]" multiple="multiple"/>
<input type="submit" name="submit" value="upload images"/>
<form/>
<?php
if( $_POST['submit'] ) {
$dir = "images/";
if(!is_dir($dir))
{
echo "Directory not found, let's create the folder.";
mkdir($dir,"0777", true);
}
$countimg = 0;
$allimg = 0;
foreach($_FILES["images"]["name"] as $k=>$name)
{
$allimg++;
$imgname = $_FILES["images"]["name"][$k];
$sizeimg = $_FILES["images"]["size"][$k];
$tmpname = $_FILES["images"]["tmp_name"][$k];
//2.
$extension = strtolower(pathinfo($dir.$imgname, PATHINFO_EXTENSION));
if($extension=='png' || $extension=='jpg' ||$extension=='jpeg' ||$extension=='gif')
{
if($sizeimg < 2097152){
if(!file_exists($dir.$imgname)){
//1.
if(move_uploaded_file($tmpname,$dir.$imgname))
{
$countimg++;
}
}
}
}
}
echo "You are trying to upload $allimg images".'<br>';
echo "From $allimg image(s) - $countimg was/were uploaded with success".'<br>';
$z = $allimg - $countimg;
echo "$z image(s) were not uploaded: Not an image, over 2MB, or already uploaded.";
}
?>
</div>
if( $_POST['submit'] ) will ensure that the form is submitted prior to running the rest of the PHP code.
I am trying to upload file using php and i get error of form not submit whenever everything is right according to me below is the code video upload form
<form action="tek.php" method="POST" enctype="multipart/form-data">
<input type="text" name="name" placeholder="Name"><br/>
<input type="text" name="mobile" placeholder="Mobile No."><br/>
<input type="file" name="videouser" ><br/>
<input type="file" name="audiouser" ><br/>
<input type="submit" name="submit" value="Submit">
</form>
and below is my tek.php page code
if(isset($_POST["submit"])){
$name = $_POST["name"];
$mobile = $_POST["mobile"];
$video_dir = "admin/video/";
$temp = explode(".", $_FILES["videouser"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
move_uploaded_file($_FILES["videouser"]["tmp_name"], "/admin/video/" .$newfilename)or die("not uploading a video");
$videofile = rand() . basename($_FILES["videouser"]["name"]);
if(move_uploaded_file($_FILES["videouser"]["name"], $video_dir.$newfilename))
{
echo "upload video successfull";
}else{
echo "video file not uploaded";
}
$audio_dir = "admin/audio/";
$audiofile = rand() . basename($_FILES["audiouser"]["name"]);
if(move_uploaded_file($_FILES["audiouser"]["name"], $audio_dir.$audiofile) or die("Not Uploaded audio"))
{
echo "upload audio successfull";
}else{
echo "audio file not uploaded";
}
}else{
echo "form not submitted.";
}
above code of tek.php page work fine for image but not for video or audio file i also increase limit post_max_size = 500M and upload_max_size = 500M where i make mistake don't know please help thanks in advance.
Change following codes:
if(move_uploaded_file($_FILES["videouser"]["tmp_name"], $video_dir.$newfilename))
{
echo "upload video successfull";
}else{
echo "video file not uploaded";
}
$audio_dir = "admin/audio/";
$audiofile = rand() . basename($_FILES["audiouser"]["name"]);
if(move_uploaded_file($_FILES["audiouser"]["tmp_name"], $audio_dir.$audiofile) or die("Not Uploaded audio"))
{
echo "upload audio successfull";
}else{
echo "audio file not uploaded";
}
you should use tmp_name instead of name in the move process
Hi Friends i got the answer
1. first configure your php.ini
2. if you use wamp then you get php.ini in wamp/bin/php/php7.0.10(php version)/php.ini
3. set in php.ini
post_max_size = 10240M
upload_max_filesize = 500M
4. Restart Your wamp server(must)
now the code to upload video below in upload.php
<?php
if(isset($_FILES['file'])){
$errors= array();
$file_name = $_FILES['file']['name'];
$file_size =$_FILES['file']['size'];
$file_tmp =$_FILES['file']['tmp_name'];
$file_type =$_FILES['file']['type'];
if (!file_exists('uploaded_here')) { // file will be uploaded in this folder
mkdir('uploaded_here', 0777, true);
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"uploaded_here/".$file_name);
echo "Uploaded in folder uploaded_here file name is : ".$file_name;
}else{
echo "Not Uploaded";
}
}
?>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post" enctype="multipart/form-data">
<input type="file" name="file" accept="file_extension|audio/*|video/*|image/*|media_type">
<br>
<input type="submit" value="Upload">
</form>
its working for me
I have a weird problem, I have a php upload code that I have implemented long ago, today I try to reuse it but it didn't work, the surprise was big when I started my Windows on VirtualBox and the exact same file that I was unable to upload from linux was uploaded for windows. I mean, the only thing that changes is the OS of client machine. Any way, I think is related to the file type restriction, because if I take out these restrictions I can upload the file from my ubuntu....
Codes are next:
form.php file:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input name="file" type="file" size="50" /><br>
<p><input name="upload" type="submit" value="upload" />
<input name="action" type="hidden" value="upload" /></p></form>
upload.php file:
<?php
$status = "";
if ($_POST["action"] == "upload") {
// data from file
$size = $_FILES["file"]['size'];
$type = $_FILES["file"]['type'];
$file = $_FILES["file"]['name'];
$pos = strpos($file, "_timetable.csv");
if ($file != "") {
if ($type == "text/csv" || $type == "application/vnd.ms-excel") {
if ($pos == false) {
$status = "Error: only files with extension <b>_timetable.csv</b> are allowed
<p><form action=\"form.php\"><input type=\"submit\" value=\" Select a different file \"></p>";
}else {
$destination = "filess/".$fitxer;
if (copy($_FILES['file']['tmp_name'],$destination)) {
$status = "<p>Uploaded: ".$file."</p>
}
else {
$status = "Error when uploading file<p><form action=\"form.php\"><input type=\"submit\"
value=\" Try again \"></p>";
}
}} else {
$status = "Error: only files with extension <b>_timetable.csv</b> are allowed
<p><form action=\"form.php\"><input type=\"submit\" value=\" Select a different file \"></p>";
}
} else {
$status = "Error when uploading file<p><form action=\"form.php\"><input type=\"submit\"
value=\" Try again \"></p>";
}
}
echo $status;
?>
I use the Following code to upload a single file .With This code I can Upload a single file to the database .I want to make multiple files uploaded by selecting multiple files in a single input type file.What Changes should i make in the code to make it upload multiple files?
<?PHP
INCLUDE ("DB_Config.php");
$id=$_POST['id'];
$fileTypes = array('txt','doc','docx','ppt','pptx','pdf');
$fileParts = pathinfo($_FILES['uploaded_file']['name']);
if(in_array($fileParts['extension'],$fileTypes))
{
$filename = $_FILES["uploaded_file"]["name"];
$location = "E:\\test_TrainingMaterial/";
$file_size = $_FILES["uploaded_file"]["size"];
$path = $location . basename( $_FILES['uploaded_file']['name']);
if(file_exists($path))
{
echo "File Already Exists.<br/>";
echo "Please Rename and Try Again";
}
else
{
if($file_size < 209715200)
{
$move = move_uploaded_file( $_FILES["uploaded_file"]["tmp_name"], $location . $_FILES['uploaded_file']['name']);
$result = $mysqli->multi_query("call sp_upload_file('".$id."','" . $filename . "','".$path."')");
if ($result)
{
do {
if ($temp_resource = $mysqli->use_result())
{
while ($row = $temp_resource->fetch_array(MYSQLI_ASSOC)) {
array_push($rows, $row);
}
$temp_resource->free_result();
}
} while ($mysqli->next_result());
}
if($move)
{
echo "Successfully Uploaded";
}
else
{
echo "File not Moved";
}
}
else
{
echo "File Size Exceeded";
}
}
}
else
{
echo " Invalid File Type";
}
?>
The Html That is used is
<form id = "upload_form" method="post" enctype="multipart/form-data" >
<input type="file" name="uploaded_file" id="uploaded_file" style="color:black" /><br/>
</form>
Basically you need to add to input name [] brackets and attribute "multiple"
<form id = "upload_form" method="post" enctype="multipart/form-data" >
<input type="file" name="uploaded_file[]" multiple="true" id="uploaded_file" style="color:black" /><br/>
</form>
Now all uploaded file will be available via
$_FILES['uploaded_file']['name'][0]
$_FILES['uploaded_file']['name'][1]
and so on
More info at
http://www.php.net/manual/en/features.file-upload.multiple.php
How can I save an image safely from a file input field using PHP & MySQL?
Here is the input file field.
<input type="file" name="pic" id="pic" size="25" />
This is a simple example, it should work.
Although you probably want to add checking for image types, file sizes, etc.
<?php
$image = $_POST['pic'];
//Stores the filename as it was on the client computer.
$imagename = $_FILES['pic']['name'];
//Stores the filetype e.g image/jpeg
$imagetype = $_FILES['pic']['type'];
//Stores any error codes from the upload.
$imageerror = $_FILES['pic']['error'];
//Stores the tempname as it is given by the host when uploaded.
$imagetemp = $_FILES['pic']['tmp_name'];
//The path you wish to upload the image to
$imagePath = "images/";
if(is_uploaded_file($imagetemp)) {
if(move_uploaded_file($imagetemp, $imagePath . $imagename)) {
echo "Sussecfully uploaded your image.";
}
else {
echo "Failed to move your image.";
}
}
else {
echo "Failed to upload your image.";
}
?>
http://php.net/file_upload covers just about everything you need to know.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$tmpFile = $_FILES['pic']['tmp_name'];
$newFile = '/new_location/to/file/'.$_FILES['pic']['name'];
$result = move_uploaded_file($tmpFile, $newFile);
echo $_FILES['pic']['name'];
if ($result) {
echo ' was uploaded<br />';
} else {
echo ' failed to upload<br />';
}
}
?>
<form action="" enctype="multipart/form-data" method="POST>
<input type="file" name="pic" />
<input type="submit" value="Upload" />
</form>