Upload file is working fine with file size less than 2.9 MB but my phpinfo (localhost) showing upload_max_filesize 64M.
When trying to upload larger files, after form submit $_POST is empty and no file where uploaded.
here is my code:
<?php
function fileUpload($attachment){
$target_file = UPLOADDIR.basename($attachment["name"]);
if (file_exists($target_file)) {
return "Sorry, file already exists.";
}
if (move_uploaded_file($attachment["tmp_name"], $target_file)) {
return "The file ". basename( $attachment["name"]). " has been uploaded.";
} else {
return $attachment;
return "Sorry, there was an error uploading your file.";
}
}
if(isset($_POST["submit"])) {
fileUpload($_FILES['fileToUpload'])
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
Before uploading script you can set maximum upload size in your php code
ini_set('upload_max_filesize', '10M');
or
You need to set the value of upload_max_filesize and post_max_size in your php.ini :
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
After that run your code
and
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
Place this at the top of your PHP script and let your script loose!
if you made any change or modifying php.ini file(s), you need to restart your HTTP server(or localhost) to use new configuration.
Related
I try to upload a file with PHP and I use the function "move_uploaded_file()" for it. Everything works well, until I try to upload a zipped CSV file. The file itself is 50MB, but once it is zipped, it's just 3MB. I can upload files until 5MB to the server, so in the case of the zip file, I thought this would work, but it doesn't.
* Uploading a zipped CSV file of 100kb does work,
* I did delete 1/3 of the lines from the CSV file, the CSV file is 28MB, and this I could upload (zipped it was 1.6MB),
* The zipped file of 3MB doesn't appear in the folder on the server. The zipped files of 100KB and 1.6MB do appear there,
* I didn't get an error message, but it does echo "Error uploading!".
Did someone face the same problems, or does someone knows a solution? Let me know! This is basicly my code;
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if (isset($_POST["submit"])) {
$file = $_FILES["file"];
$filename = $file["name"];
$target_dir = "data/";
$target_file = $target_dir . basename($filename);
$file_type = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if (move_uploaded_file($file["tmp_name"], $target_file)) {
echo "Done uploading!";
} else {
echo "Error uploading!";
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<div class="custom-file mb-3">
<input type="file" class="custom-file-input" id="file" name="file" required accept=".csv,.zip" />
</div>
<p class="text-right">
<button class="btn btn-primary" name="submit" type="submit">Upload!</button>
</p>
</form>
Let me know if you need something. All the help and ideas are appericiated. Thanks!
Edit;
I did got an error in $_FILES['file']['error'];
I got error "1", which reffers to: 'The uploaded file exceeds the upload_max_filesize directive in php.ini'
https://www.php.net/manual/en/features.file-upload.errors.php
I did "echo phpinfo();", and found out that upload_max_filesize is only 2MB instead of the 5MB that I thought I configurated.
The code below is used for uploading video files to a MySQL database or copying them to folders. In this code small videos are uploaded but large files do not work. I am using phpmysql. What do I miss?
<?
ob_start();
?>
<html>
<head>
</head>
<body>
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Upload !">
</form>
</body>
</html>
<?
include('connection.php');
{
if (isset($_POST['submit']))
{
$name= $_FILES['file']['name'];
$temp= $_FILES['file']['tmp_name'];
$tp= $_FILES['file']['type'];
move_uploaded_file($temp,"uploaded/".$name);
$url = "";
$insert = mysql_query
("INSERT INTO 'videos' VALUE ('','$name','$url')");
if ($insert)
{
echo "has been uploaded";
}
}
}
?>
You need to increase the following values in your php.ini
max_input_time = 300
max_execution_time = 600
memory_limit = 1024M
upload_max_filesize = 1512M
post_max_size = 2048M
(You have to change the values accordingly to your needs)
Go into your php.ini setting and change
; Maximum allowed size for uploaded files.
upload_max_filesize = #number
; Must be greater than or equal to upload_max_filesize
post_max_size = #number
If you haven't changed it, then it is preventing big file sizes. I did the same with huge megapixel images and found the cause to be that.
To prevent it from being an issue, set it to 0.
I have a problem. I'm writng a simple form for upload file to server. I can send any file without mp3, I don't know why.
add2.php:
{
$max_size = 104857600;
if (is_uploaded_file($_FILES['plik']['tmp_name'])) {
if ($_FILES['plik']['size'] > $max_size) {
echo 'Error! File is too big!';
} else {
echo 'I have file, name: '.$_FILES['plik']['name'];
$nazwa= $_FILES['plik']['name'];
mysql_query("INSERT INTO files (name) values ('{$nazwa}') ");
echo '<br/>';
if (isset($_FILES['plik']['type'])) {
echo 'Typ: '.$_FILES['plik']['type'].'<br/>';
}
move_uploaded_file($_FILES['plik']['tmp_name'],
$_SERVER['DOCUMENT_ROOT'].'/music/'.$_FILES['plik']['name']);
}
} else {
echo 'Error with sending file!';
}
When i try to send mp3 i get "Error with sending file!".
EDIT:
<form action="add2.php" method="POST" ENCTYPE="multipart/form-data">
<input type="file" name="plik"/><br/>
<input type="submit" value="Send file"/>
</form>';
I tested on my machine and see:
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.
Font: How to upload mp3 files
What should I do if I want to upload an image to a file system such as a folder in my domain? The thing is, I've tried uploading image on a localhost and it works fine but when i did it in my domain it doesn't work anymore. Don't really know what's the problem here. any help will do! Thanks anyway.
<form action="adminUploadTry.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" >
<input type="submit" name="submit" value="Upload" >
</form>
<?php
if(isset($_POST['submit'])){
$image_name = $_FILES['image']['name'];
$image_type = $_FILES['image']['type'];
$image_size = $_FILES['image']['size'];
$image_tmp_name = $_FILES['image']['tmp_name'];
if($image_name==''){
echo "You forgot to select an image. Please choose one!";
exit();
}
else
move_uploaded_file(image_tmp_name, "/home/stagcon2/public_html/StagConnect/admin/pictures/$image_name");
echo "Image Succesfully Uploaded!";
echo "<img src='/home/stagcon2/public_html/StagConnect/admin/pictures/$image_name'>";
}
?>
by the way, the problem here is that after i uploaded an image it will say "successfully uploaded" but when I check my folder the image was not saved.
May be you forget $ sign in image_tmp_name at
move_uploaded_file($image_tmp_name, "/home/stagcon2/public_html/StagConnect/admin/pictures/$image_name");
See if you need to increase the value of upload_max_filesize and post_max_size in your php.ini :
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
I have a very simple upload script. Here is my HTML file that allows a user to submit a file:
<form enctype="multipart/form-data" action="uploader.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
And then here is my PHP file that actually places the file on my server
<?php
echo "starting the upload initially";
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
I have the html saved at url.com/uploading.html and the php saved at url.com/uploader.php
After loading the HTML page and putting a file in the uploader the browser redirects me to the php file but then doesn't do anything further, not even print out that initial statement that I have. Do you see any problems? All of the permissions are 777 so that shouldn't be it. Could there be any other things I need to take care of on my server? Everything is in the same folder too.
Thanks!
Could you verify that post_max_size and upload_max_filesize is greater than or equal to the size of the file you're attempting to upload?
Is it possible your max size is > than the size of your file