PHP move_uploaded_file() doesn't upload .zip of 3MB to server - php

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.

Related

Image not uploading to web server

I am having some difficulty uploading an image to a folder on my web server. Here's my code:
HTML:
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="Upload Image" name="upload">
</form>
PHP:
<?php
if (isset($_POST["upload"])) {
$name = $_FILES["image"]["name"];
$type = $_FILES["image"]["type"];
$size = $_FILES["image"]["size"];
$temp = $_FILES["image"]["tmp_name"];
$error = $_FILES["image"]["error"];
$target_file = "/profiles/images/$name";
move_uploaded_file($temp, $target_file);
}
?>
I've tried echoing out the file name ($name), but it doesn't return anything at all. It's blank for some reason. This is when I try to upload an image. When I echo $target_file, I get this "/profiles/images/", the $name part is not included for some reason.
Check your max upload file size. If in the settings the size is smaller than the file you are trying to upload, nothing is send.
This is happens in those cases when we try to upload very heavy size image, Please try to check your maximum file size, and please for testing try to upload maximum 100kb size of pic through this code. Hope this will work.

PHP file upload not working - tried lots of tutorial websites, permissions correct, php.ini correct

I am hosting a file upload script on an digital ocean server. I have copied line for line tutorials from all over the internet and changed my permissions using CHMOD 777. I cannot for the life of me figure out why the file won't upload.
PHP
<?php
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
HTML
<form enctype="multipart/form-data" action="upload_manager.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
The file I'm trying to upload is only a few hundred KB and is a PNG. I have tried JPEG too.
When I print out the value of
$_FILES['uploadedfile']['tmp_name']
It is empty
The value of
$_FILES['uploadedfile']['name']
is the file name.
I had a look at my php ini and everything related to uploading files is correct.
What else can I try to get this to work?

uploading videos into a folder and its link to database using PHP/MySQL

I am trying to upload a video and save it in a folder as well as savings its path in the database, but the videos are not inserting into the specific folder.
I have searched a lot and found some code. The code is working for images. I have done some modifications from images to videos, but that didn't work.
Here is the parts of my code.
<form action="" method="post" enctype="multipart/form-data">
<div class="form_search">
<label>Upload Video Profile:</label>
<span class="form_input">
<input type="file" name="uploadvideo" />
</span>
</div>
<div class="form_search">
<label> </label>
<span class="form_input">
<input type="submit" name="submitdetails" value="Upload" class="button"/>
</span>
</div>
</form>
and my php code to upload video is
<?php
if(isset($_POST['submitdetails']))
{
$name=$_FILES['uploadvideo']['name'];
$type=$_FILES['uploadvideo']['type'];
//$size=$_FILES['uploadvideo']['size'];
$cname=str_replace(" ","_",$name);
$tmp_name=$_FILES['uploadvideo']['tmp_name'];
$target_path="company_profile/";
$target_path=$target_path.basename($cname);
if(move_uploaded_file($_FILES['uploadvideo']['tmp_name'],$target_path))
{
echo "hi";
echo $sql="UPDATE employer_logindetails SET (video) VALUES('".$cname."')";
$result=mysql_query($sql);
echo "Your video ".$cname." has been successfully uploaded";
}
}
?>
Please help me where I am going wrong.
All my php.ini modifications are done, and video size is only 7mb.
Step-1
This script will allow you to upload files from your browser to your hosting, using PHP. The first thing we need to do is create an HTML form that allows people to choose the file they want to upload.
<form enctype="multipart/form-data" action="upload.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
This form sends data to the file "upload.php", which is what we will be creating next to actually upload the file.
Step-2
The actual file upload is very simple:
<?php
$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.";
}
?>
This very small piece of code will upload files sent to it by your HTML form.
The first line $target = "upload/"; is where we assign the folder that files will be uploaded to. As you can see in the second line, this folder is relative to the upload.php file. So for example, if your file was at www.yours.com/files/upload.php then it would
upload files to www.yours.com/files/upload/yourfile.gif. Be sure you remember to create
this folder! with 777 rights
Step-3
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
Assuming that you didn't change the form field in our HTML form (so it is still named uploaded), this will check to see the size of the file. If the file is larger than 350k, they are given a file too large error, and we set $ok to equal 0.
You can change this line to be a larger or smaller size if you wish by changing 350000 to a different number. Or if you don't care about file size, just leave these lines out
We are not using $ok=1; at the moment but we will later in the tutorial.
We then move the uploaded file to where it belongs using move_uploaded_file (). This places it in the directory we specified at the beginning of our script. If this fails the user is given an error message, otherwise they are told that the file has been uploaded.
Putting All Together
<?php
$target = "upload/";
$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";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
Obviously if you are allowing file uploads you are leaving yourself open to people uploading lots of undesirable things. One precaution is not allowing them to upload any php, html, cgi, etc. files that could contain malicious code. This provides more safety but is not sure fire protection.
Try this: $_FILES['userfile'] in place of $_FILES['uploadvideo'] everywhere.
I don'e see any name with uploadvideo in your form.
But in php you are using $_FILES['uploadvideo']
Try giving the exact name of the file input
e.g.:
<input type="file" name="uploadvideo">

PHP Upload Wont Work, But Wordpress Upload Does

I am creating my own upload script to upload files to my webserver (like small flash videos). However, it does not seem to be working. However, I don't think it is PHP because uploading files to my server via Wordpress does work.
Here is the upload form:
<form enctype="multipart/form-data" action="upload.php" method="POST">
Video: <input name="vid" type="file" />
<br/>
Name: <input name="title" type="text" />
<br/>
Description:
<br/>
<br/>
<textarea name="desc">Your description here</textarea>
<br/>
<br/>
<input type="submit" value="Upload" />
</form>
Here is the upload PHP page:
<?php
$target = basename( $_FILES['uploaded']['name']) ;
$ok=1;
//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";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
I have set the max upload size in php.ini to 100MB, however, the upload still fails. My php.ini file can be viewed here at pastebin. Also note that I have removed all verification from the upload script to see if it works.
From the looks of your code, you are uploading the file in the same directory as the running script. Are you sure you have write permission for the webserver in that directory?
Try uploading the file in your temp directory:
move_uploaded_file($_FILES['uploaded']['tmp_name'], sys_get_temp_dir().DIRECTORY_SEPARATOR.$target);
and see if it works
UPDATE
You are calling your input file "vid" but you are trying to access the input "uploaded".
replace all your $_FILES occurences with $_FILES['vid'][]...
And enable error reporting on your machine
How could you get the value of video from this:
$target = basename( $_FILES['uploaded']['name']) ;
WHile the 1st block contain the name of the file which was given to the html.
IN your case it is : Video: <input name="vid" type="file" /> Vid
SO use this on your code:
<?php
$target = basename( $_FILES['vid']['name']) ;

Basic PHP Upload script with no result

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

Categories