$image = $_FILES['picture']['name'];
$id=$_SESSION['id'];
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
$ext = findexts ($_FILES['picture']['name']) ;
//This assigns the subdirectory you want to save into... make sure it exists!
$target = "mainimage/";
//This combines the directory, the random file name, and the extension
$target = $target . $id.".".$ext;
if(move_uploaded_file($_FILES['picture']['tmp_name'], $target))
{
echo ("This is your new profile picture!");
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
for some reason the "else" keeps showing up (there was a problem uploading the file). I put comments in everything that I am trying to do. please help! thanks!
To close the question as per OP's request.
The issue is that you need to increase the maximum size for uploads. PHP's default is usually 2M and you are most probably trying to upload a file larger than what the maximum setting is set to.
This can be achieved in two ways.
By editing your php.ini file with the following settings:
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
or via an .htaccess file placed in the root of your server:
php_value upload_max_filesize 20M
php_value post_max_size 30M
Related
I change the value but it's not working, how I upload a 50mb video file with 15 mint duration? I have no php.ini file
$max_allowed_size = $system['max_video_size'] * 5120;
// prepare uploads directory
$folder = 'videos';
$directory = $folder.'/'. date('Y') . '/' . date('m') . '/';
if($_POST["multiple"] == "true") {
// prepare files
$files = array();
foreach($_FILES['file'] as $key => $val) {
for($i=0; $i < count($val); $i++) {
$files[$i][$key] = $val[$i];
}
}
$return_files = array();
$files_num = count($files);
I changed the value 5120 but I can't upload over 2mb video file size.
Add in folder file: .user.ini with
upload_max_filesize = 50M
post_max_size = 200M
Or test file size
if($_FILES['file']['size'] > $bytes){ // upload ... }
https://www.php.net/manual/en/configuration.file.per-user.php
https://www.php.net/manual/en/ini.list.php
https://www.php.net/manual/en/reserved.variables.files.php
To increaes file upload size in PHP, you need to modify the upload_max_filesize and post_max_size variable’s in your php.ini file.
upload_max_filesize = 10M
post_max_size = 10M
Then after don't forget to run below command
service apache2 restart
I am uploading image files in PHP but I found out that I was unable to get a file size of some images captured from one mobile device. I can get the file name but not the size so can anyone tell what likely will be the problem or how to fix it to be able to get the file size?
When I check the size in my system its size is in MB while other of my images are in KB.
<?php
if (isset($_FILES["file"]["name"][1]) && !empty($_FILES["file"]["name"])) {
$fileext = ["jpg", "jpeg", "png", "bif", "gif", "bmp"];
for ($i = 1; $i < count($_FILES["file"]["name"]); $i++) {
$name = $_FILES["file"]["name"][$i];
$type = $_FILES["file"]["type"][$i];
$size = $_FILES["file"]["size"][$i];
$path = $_FILES["file"]["tmp_name"][$i];
$ext = end(explode('.', strtolower($name)));
//checking file supported type
if (in_array($ext, $fileext)) {
echo $name . "<br>";
} else {
echo "file type not supported!";
}
}
}
?>
As for the file upload error:
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 modifying php.ini file(s), you need to restart your HTTP server to use new configuration.
if you'r unable to access your php.ini you can try:
ini_set('upload_max_filesize', '10M');
ini_set('post_max_size', '10M');
ini_set('max_input_time', 300);
ini_set('max_execution_time', 300);
also incressing memory_limit might be usefull..
I'm a PHP newbie and have written a script to process form uploads. It works for small files(less than 1Mb). However when I try to upload an ~4Mb pdf file it returns an error message. What am I doing wrong?
PS: I ran php_info, got the value of "upload_tmp_dir" and set it to a directory owned by the apache process(www-data)
My client-side code is
<form action="upper.php" method="post" enctype="multipart/form-data">
<input type="file" name="myFile"><br>
<input type="submit" value="Upload">
</form>
Upper.php contains
define("UPLOAD_DIR", "/var/www/html/upload/");
if (!empty($_FILES["myFile"])) {
$myFile = $_FILES["myFile"];
if ($myFile["error"] !== UPLOAD_ERR_OK) {
echo "Error is " . $myFile["error"];
//echo "<p>An error occurred.</p>";
exit;
}
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
// don't overwrite an existing file
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
$i++;
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}
// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"], UPLOAD_DIR . $name);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
}
// set proper permissions on the new file
chmod(UPLOAD_DIR . $name, 0644);
}
Most probably you have wrong config in you php.ini file.
You need to set
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
its already described here: PHP change the maximum upload file size
You ned to set values in php.in file.
Open the file,
Find upload_max_filesize and post_max_size lines.
Change this two value. (M= MB)
; Maximum allowed size for uploaded files.
upload_max_filesize = 20M
; Must be greater than or equal to upload_max_filesize
post_max_size = 20M
Restart the sever for effect.
Note:
You can set any number. But not use any decimal value.
Open the php.ini file in NOTEPAD. Don't user wordpad or any word processor.
I want to upload files from my server. I had written this piece of code months ago when it worked fine, but now I have no idea what's going wrong.
I basically want to move the file to a folder "uploads" in my server and then store the path in the database.
The uploaded file doesn't get reflected in the database.
if(isset($_POST['submit']))
{
require("dbconn.php");
$filename = $_POST['filename'];
$name = $filename . "." . pathinfo($_FILES['ufile']['name'],PATHINFO_EXTENSION);
//$name = $_FILES['ufile']['name'];
//$size = $_FILES['file']['size']
//$type = $_FILES['file']['type']
$tmp_name = $_FILES['ufile']['name']; //was tmp_name
$error = $_FILES['ufile']['error'];
if(isset($name))
{
if(!empty($name))
{
$location = 'uploads/';
if(move_uploaded_file($tmp_name, $location.$name))
{
echo "hi";
$filename = $_POST['filename'];
$filepath = $location.$name;
$advname = $_POST['advname'];
$year = $_POST['year'];
$cname = $_POST['cname'];
$ctype = $_POST['ctype'];
$sqlq = "INSERT INTO file(filename, filepath, advname, year, cname, ctype) VALUES ('".$filename."','".$filepath."','".$advname."','".$year."','".$cname."','".$ctype."');";
$result = mysql_query($sqlq);
if(!$result)
{
die("Error in connecting to database!");
}
}
}
}
}
There seems to be a problem in the
if(move_uploaded_file($tmp_name, $location.$name))
statement. This condition is evaluated as false.
If $_FILES['ufile']['error'] gives you a value of 1 then the upload_max_filesize in your config is set to a smaller value that the file you are trying to upload.
You cannot change the upload_max_filesize from within your code.
If you have access to your php.ini, .htaccess, httpd.conf or .user.ini you can change this value in any of those files.
So you would need to add or modify the parameter from its default of 2M (2meg)
upload_max_filesize = 20M
Its also a good idea to check the post_max_size at the same time as this may also have an effect on the max size of the file uploaded, make sure post_max_size is larger than upload_max_filesize as this should allow extra space for the other fields that you will be uploading as part of the post
I am facing problem uploading files, getting errors after some time.
I am uploading files and inserting details into database about image like name date etc.
What happens is that till no of uploaded file is 27 everything works
well but when I try to upload more than 27 file it start showing errors
like
Warning: move_uploaded_file(../../images/2015/05/imvsa/kexk.jpg):
failed to open stream: No such file or directory
Warning: move_uploaded_file(): Unable to move
'D:\wamp\tmp\php3635.tmp' to '../../images/2015/05/imvsa/kexk.jpg'
my php.ini
max_execution_time = 1440
max_input_time = 1440
post_max_size = 1024M
upload_max_filesize = 1024M
max_file_uploads = 10000
session.save_path = "d:/wamp/tmp"
session.gc_maxlifetime = 7200
memory_limit = 512M
If I truncate my database table then it starts working uptill row 27 and again it starts to fail.
my script
if($_FILES['image']['size']<5242880&&getimagesize($_FILES['image'])!=false)
{
if(!is_dir('../../images/2015/05/'))
{
$mkdir = mkdir('../../images/2015/05/', 0777, true);
}
$info = pathinfo($_FILES['image']['name']);
$image = $_FILES['image']['name'];
$ext = $info['extension'];
$temp_file = $_FILES['image']['tmp_name'];
$img_target ='../../images/2015/05/'.$image.'.'.$ext;
$upload = move_uploaded_file($temp_file, $img_target);
}
// and now I do Insert in database
Please see and suggest any possible way to make it work, I an going to be uploading hundreds of image in a day with max size of 5MB.
Thanks
Try this:
if(!is_dir('../../images/2015/05/imvsa/')){
$mkdir = mkdir('../../images/2015/05/imvsa/', 0777, true);
}