I'm trying to upload a mp4 file with php, and I succeed it, but after that, the file can't be run with VLC, even though it could be run before upload. The error message says that the file can't be opened gives me the path of the file and ends with (Bad File Descriptor).
I've made the following configurations in php.ini file:
file_uploads = On
upload_max_filesize = 25M
post_max_size = 25M
Here is my code:
if ($_FILES["video"]["name"] == "") {
$error = "No video imported.";
}
else {
if (file_exists("uploads/" . $_FILES["video"]["name"])) {
$error = "The file already exists.";
}
else if ($_FILES["video"]["type"] != "video/mp4") {
$error = "File format not supported.";
}
else if ($_FILES["video"]["size"] > 26214400) {
$error = "Only files <= 25ΜΒ.";
}
else {
move_uploaded_file($_FILES["video"]["tmp_name"], "uploads/" . $_FILES["video"]["name"]);
}
}
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data">
<fieldset>
<div class="area">
<label for="path">Select file:</label>
<input class="upload" type="file" name="video"></input>
<span><?php echo $error; ?></span><br />
</div>
</fieldset>
<input type="submit" name="insert" value="upload"></input>
</form>
You had a syntax error on line 4 & 5. It should be
} elseif (file_exists("uploads/" . $_FILES["video"]["name"])) {
Not:
} else {
if (file_exists("uploads/" . $_FILES["video"]["name"])) {
This code has been tested and is working.
<?php
if ($_FILES["video"]["name"] == "") {
$error = "No video imported.";
} elseif (file_exists("uploads/" . $_FILES["video"]["name"])) {
$error = "The file already exists.";
} elseif ($_FILES["video"]["type"] != "video/mp4") {
$error = "File format not supported.";
} elseif ($_FILES["video"]["size"] > 26214400) {
$error = "Only files <= 25??.";
} else {
move_uploaded_file($_FILES["video"]["tmp_name"], "uploads/" . $_FILES["video"]["name"]);
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data">
<fieldset>
<div class="area">
<label for="path">Select file:</label>
<input class="upload" type="file" name="video"></input>
<span><?php echo $error; ?></span><br />
</div>
</fieldset>
<input type="submit" name="insert" value="upload"></input>
</form>
Related
I'm trying to create a form to upload a file, the problem is that the file won't be uploaded. in my code it returns "Image not uploaded".
I've searched a lot online and all the examples uses the same code.
Code:
<?php
if (isset($_FILES['image_url']) && is_uploaded_file($_FILES['image_url']['tmp_name'])) {
$is_img = getimagesize($_FILES['image_url']['tmp_name']); //Is an image?
if (!$is_img) {
$userfile_name = "It isn't an image";
}
else {
if (!file_exists("/images/products/" . $_FILES['image_url']['name'])) {
$uploaddir = '/images/products/';
$userfile_tmp = $_FILES['image_url']['tmp_name'];
$userfile_name = $_FILES['image_url']['name'];
move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name);
}
else {
$userfile_name = $_FILES['image_url']['name'];
}
}
}
else {
$userfile_name = "Image not uploaded";
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?> " enctype=”multipart/form-data”>
<p><label for="image">Immagine: </label>
<input type="file" name="image_url"/></p>
<p><input type="submit" value="Salva" /></p>
</form>
The form has also other fields and the data are correctly send to the server.
Try this
<?php
if (isset($_FILES['image_url']) && is_uploaded_file($_FILES['image_url']['tmp_name'])) {
$is_img = getimagesize($_FILES['image_url']['tmp_name']); //Is an image?
if (!$is_img) {
$userfile_name = "It isn't an image";
}
else {
if (!file_exists("images/products/" . $_FILES['image_url']['name'])) {
$uploaddir = 'images/products/';
$userfile_tmp = $_FILES['image_url']['tmp_name'];
$userfile_name = $_FILES['image_url']['name'];
move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name);
}
else {
$userfile_name = $_FILES['image_url']['name'];
}
}
}
else {
$userfile_name = "Image not uploaded";
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?> " enctype="multipart/form-data">
<p><label for="image">Immagine: </label>
<input type="file" name="image_url"/></p>
<p><input type="submit" value="Salva" /></p>
</form>
my directory is looks like this
->source files
--css
--upload
add_file.php
upload.php
my code is as follows
upload.php
<form role="form" method="post" enctype="multipart/form-data" action="add_file.php">
<div class="form-group">
<label for="filecaption">
Caption :
</label>
<input type="text" name="f_caption" class="form-control"/>
</div>
<div class="form-group">
<label for="Choose a File">
Caption :
</label>
<input type="file" name="uploaded_file" class="form-control"/>
</div>
<div class="form-group">
<input type="submit" name="upload" value="Upload" class="form-control btn btn-warning"/>
</div>
</form>
add_file.php
<?php
if(isset($_FILES['uploaded_file']))
{
$f_name= $_FILES['uploaded_file']['name'];
$temp_name= $_FILES['uploaded_file']['tmp_name'];
if(!$temp_name)
{
die("no file uploaded..please try again");
}
else
{
$path = "upload/" . $f_name;
if( move_uploaded_file($f_name, $path))
{
echo "success";
}
else
{
echo "failure";
}
}
}
?>
The problem is from the add.php.
Try this
<?php
if(isset($_FILES['uploaded_file'])) {
$f_name= $_FILES['uploaded_file']['name'];
$temp_name= $_FILES['uploaded_file']['tmp_name'];
if(!$temp_name)
{
die("no file uploaded..please try again");
}
else
{
$path = "upload/" . $f_name;
if( move_uploaded_file($temp_name, $path))
{
echo "success";
}
else
{
echo "failure";
}
}
}
?>
The problem is that you were moving from the f_name instead of the temp_location.
Im trying to upload multiple files but my code is bypassing the "move_uploaded_file" code. What is missing?
foreach ($_FILES['file']['name'] as $file) {
$target_dir = "uploads/";
$target_file = $target_dir . $file;
if (move_uploaded_file($file, $target_file)) {
echo "The file ".$_FILES["file"]["name"]. " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
move_uploaded_file needs to get the temporary file name: $_FILES['file']['tmp_name']
foreach ($_FILES as $file) {
$target_dir = "./";
$target_file = $target_dir . $file['name'];
if (move_uploaded_file($file['tmp_name'], $target_file)) {
echo "The file " . $file["name"] . " has been uploaded.<br />";
} else {
echo "Error uploading the file " . $file["name"] . ".<br />";
}
}
Small HTML snippet for the upload form:
<form action="./testpage.php" method="post" enctype="multipart/form-data">
<input name="file1" type="file" /><br />
<input name="file2" type="file" /><br />
<input type="submit" value="Upload!" />
</form>
Try this:
for($i=0; $i < count($_FILES['file']['tmp_name']);$i++)
{
if(!is_uploaded_($_FILES['file']['tmp_name'][$i]))
{
$messages[] = 'No uploaded';
}
else
{
if(#copy($_FILES['file']['tmp_name'][$i],$target.'/'.$_FILES['file']['name'][$i]))
{
$messages[] = $_FILES['file']['name'][$i].' uploaded';
}
else
$messages[] = 'Uploading '.$_FILES['file']['name'][$i].' Failed';
}
}
}
html:
<form enctype="multipart/form-data" action="#" method="post">
<input id="uploadFile" name="file[]" type="file" />
<input id="uploadFile" name="file[]" type="file" />
<input type="submit" value="Upload" name="uploadt" />
</form>
Here is my file-upload script, and i am getting the following error
Notice: Undefined index: fupload in C:\Users\Tuskar\Desktop\Projekt\htdocs\Project IT-Space\Profile\edit_profile_parse.php on line 8
But according there should not error, because i identified the index. It seems i don't have access to the $_FILES array, because before i got this error ive been getting other similar errors or the programm completely passes the if and goes directly to the else (file not chosen)
I know the script is primitive and includes almost no security, but i just want it to work first before i add other features like max file size or file restriction ... :(
Here is the code i am using.
Upload Picture
<form action="edit_profile_parse.php" method="get" enctype="multipart/form-data" >
<input type="hidden" name="MAX_FILE_SIZE" value="999999999"> </input>
<input type="file" name="fupload"> </input>
<input type="submit" name="submit" value="Upload"> </input>
</form>
Here is the php that handles the form
if (isset( $_GET['submit'] ))
{
if (isset($_FILES['fupload'] ))
{
echo "name: ".$_FILES['fupload']['name']." <br> ";
echo "size: ".$_FILES['fupload']['sizw']." <br> ";
echo "type: ".$_FILES['fupload']['type']." <br> ";
if ($_FILES['fupload']['type'] == "image/gif")
{
$source = $_FILES['fupload']['tmp_name'];
$target = "images/" .$_FILES['fupload']['name'];
move_uploaded_file($source, $target) or die ("Error: " .mysql_error());
$size = getImageSize($target);
$imgstr = "<img src=\" '".$target."' \">";
echo $imgstr;
}
else
{
echo "Problem uploading the file ... ";
}
}
else
{
echo "No file chosen !! ";
}
}
else
{
echo "Button not clicked ";
}
You should use form method to POST instead of get.
<form action="edit_profile_parse.php" method="post" enctype="multipart/form-data" >
Make sure your FORM tag has method="POST". GET requests do not support multipart/form-data uploads.
I hope this works:
the form:
<form action="edit_profile_parse.php" method="post" enctype="multipart/form-data" >
<input type="hidden" name="MAX_FILE_SIZE" value="999999999"> </input>
<input type="file" name="fupload"> </input>
<input type="submit" name="submit" value="Upload"> </input>
</form>
the php file:
<?php
if($_POST) {
$max_size = mysql_real_escape_string(strip_tags($_POST['MAX_FILE_SIZE']));
$file = $_FILES['fupload']['name'];
if(isset($max_size) && !empty($max_size) && !empty($file)) {
$file_type = $_FILES['fupload']['type'];
$tmp = $_FILES['fupload']['tmp_name'];
$file_size = $_FILES['fupload']['size'];
$allowed_type = array('image/png', 'image/jpg', 'image/jpeg', 'image/gif');
if(in_array($file_type, $allowed_type)) {
if($file_size < $max_size) {
$path = 'images/'.$file;
move_uploaded_file($tmp, $path);
//if you want to store the file in a db use the $path in the query
} else {
echo 'File size: '.$file_size.' is too big';
}
} else {
echo 'File type: '.$file_type.' is not allowed';
}
} else {
echo 'There are empty fields';
}
}
?>
Upload Picture
<form action="edit_profile_parse.php" method="POST" enctype="multipart/form-data" >
<input type="hidden" name="MAX_FILE_SIZE" value="999999999"> </input>
<input type="file" name="fupload"> </input>
<input type="submit" name="submit" value="Upload"> </input>
</form>
PHP file
<?php
if (isset( $_POST['submit'] ))
{
if (isset($_FILES['fupload'] ))
{
echo "name: ".$_FILES['fupload']['name']." <br> ";
echo "size: ".$_FILES['fupload']['size']." <br> ";
echo "type: ".$_FILES['fupload']['type']." <br> ";
if ($_FILES['fupload']['type'] == "image/gif")
{
$source = $_FILES['fupload']['tmp_name'];
$target = "images/" .$_FILES['fupload']['name'];
move_uploaded_file($source, $target) or die ("Error: " .mysql_error());
$size = getImageSize($target);
$imgstr = "<img src=\" '".$target."' \">";
echo $imgstr;
}
else
{
echo "Problem uploading the file ... ";
}
}
else
{
echo "No file chosen !! ";
}
}
else
{
echo "Button not clicked ";
}
?>
there is a little form on one page which has the code below,
<div class="postcomment">
<form id="comments" action="insertcomment.php" method="POST" enctype="multipart/form-data">
Comment: <input type="text" name="comment" id="commentfield">
<input type="submit" name="submit" value="Post comment" class="button">
<br>
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
Image: <input type="file" name="image" />
<br>
</div>
once the user adds the picture by browsing for it the form then goes to the insertcomment.php code which is below
$target_path = "images/";
$file_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $file_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
and for some reason it is displaying the error and not showing in the images directory, the error is: Parse error: syntax error, unexpected T_IF in /homepages/21/d417005970/htdocs/rk8479/htdocs/insertcomment.php on line 18
Try this
HTML
<div class="postcomment">
<form id="comments" action="insertcomment.php" method="POST" enctype="multipart/form-data">
Comment: <input type="text" name="comment" id="commentfield">
<br>
Image: <input type="file" name="image" />
<br>
<input type="submit" name="submit" value="Post comment" class="button">
</form>
</div>
PHP CODE (you are trying to use it on same page you can check for if(isset($_POST['submit']))
if($_FILES['image']['size'] > 0){
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["image"]["name"]));
if ((($_FILES["image"]["type"] == "image/gif")
|| ($_FILES["image"]["type"] == "image/jpeg")
|| ($_FILES["new_image"]["type"] == "image/png")
|| ($_FILES["image"]["type"] == "image/pjpeg"))
&& ($_FILES["image"]["size"] < 1048576)
&& in_array($extension, $allowedExts))
{
if ($_FILES["image"]["error"] > 0)
{
$error_message = $_FILES["image"]["error"];
}
else
{
if (file_exists("images/" . $_FILES["image"]["name"]))
{
$error_message = $_FILES["image"]["name"] . " " . $LANG['image_exist'];
}
else
{
if(move_uploaded_file($_FILES["image"]["tmp_name"],
"images/" . $_FILES["image"]["name"])) {
// success
$image_name = $_FILES["image"]["name"];
} else {
$error_message = "Upload Failed!";
}
}
}
}
else
{
$error_message = "Error: May be different ext or size";
}
}