PHP uploading a file not working - php

The code below is to upload files to a local folder. Once I select a file from the Dialog Box, it should display "OK" to confirm selection of file. But the code below is not working as required.
<?php
if(isset($_FILES['file']['name']))
$name = $_FILES['file']['name'];
if(isset($_FILES['file']['tmp_name']))
$tmp_name = $_FILES['file']['tmp_name'];
if(isset($name))
{
if(!empty($name))
echo 'OK';
else
echo 'Please chose a file';
}
?>
<form action="up.php" method="POST" encrypt="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="submit" value="Submit">
</form>

First try:
var_dump($_FILES);
If you are not seeing anything in there on post, then you may need to set the following in your php.ini file:
file_uploads = On;
You may also need to change the allowed file sizes in the php.ini file if the file you are trying to upload is bigger than what is allowed:
post_max_size = 8M; make larger if needed
upload_max_filesize = 8M; make larger if needed*
After making these changes make sure to restart your webserver (apache/nginx.)
* upload_max_filesize should never be bigger than post_max_size
......

Related

I cannot upload files > 100K

I am not able to upload files larger than 100KB. Uploading multiple smaller files is OK. This is LAMP running on Fedora 27.
Here are the pertinent code snippets. The return value is "2", which I believe means that the file is too big. I've already set and adjusted the usual suspects in php.ini. I am not getting to $mail->AddAttachment so I don't expect that it is a phpmailer issue.
Front End Code :
<form method="post" enctype="multipart/form-data"\>
<input name="file_arr[]" id='userfiles' type="file" multiple="multiple"/ >
<input type='submit' name='submit' id='submit' value='Send' />
Back End Code :
$mail = new PHPMailer;
.
foreach ($_FILES["file_arr"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["file_arr"]["tmp_name"][$key];
$fname = $_FILES["file_arr"]["name"][$key];
$mail->AddAttachment($tmp_name, $fname);
$fcnt++;
} else {
return($error);
}
}
Adjust php.ini
You need to check php.ini to see what your max file upload size is set to. You can adjust memory_limit, upload_max_filesize, post_max_size variables accordingly.
Get php.ini Location
To find your php.ini, run phpinfo() and echo the results: echo phpinfo();
You may or may not need to echo phpinfo().

Failed multi upload when images size goes height

I'm using php to upload my pics but when my selected images size goes to height
my upload failed. On the page no error show for this happend This is like refresh page but my selected images size is lower than 3 MG it Works Well
whats my problems.
PLEASE HELP ME.
$output_dir = "../PostImage/";
if(isset($_FILES["myfile"]))
{
$ret = array();
$error =$_FILES["myfile"]["error"];
{
if(!is_array($_FILES["myfile"]['name'])) //single file
{
$RandomNum = time();
$ImagePostName=jdate("HisYmd",$timestamp)."".$RandomNum."".convert_filename_to_md5($_POST['title'])."".$_FILES["myfile"]["name"];
move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $ImagePostName);
}
else
{
$fileCount = count($_FILES["myfile"]['name']);
for($i=0; $i < $fileCount; $i++)
{
$RandomNum = time();
$ImagePostName=jdate("HisYmd",$timestamp)."".$RandomNum."".convert_filename_to_md5($_POST['title'])."".$_FILES["myfile"]["name"][$i];
move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$ImagePostName );
}
}
}
}
<form name="form1" method="post" action="index.php" enctype="multipart/form-data">
<input class="form-control input-lg m-bot15" name="myfile[]" id="myfile" multiple="multiple" type="file"/>
<input type="submit" value="upload" placeholder=""/>
</form>
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 can't change your php.ini, you're out of luck. You cannot change these values at run-time; uploads of file larger than the value specified in php.ini will have failed by the time execution reaches your call to ini_set.
If you can't change your php.ini, you're out of luck. You cannot change these values at run-time; uploads of file larger than the value specified in php.ini will have failed by the time execution reaches your call to ini_set.

File upload in php not working on remote server

Here is the code which I am using
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file"><br><br>
<input type="submit" value="submit" name="submit">
</form>
PHP code :
<?php
$name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
$location = "/var/www/tmp/";
if(move_uploaded_file($tmp_name, $location.$name)){
echo 'File uploaded successfully';
} else {
echo 'You should select a file to upload !!';
}
?>
I checked the permissions of the folder as well as checked the php.ini file but still always I am getting 'You should select a file to upload '
Can anybody please help me out on this issue ?
Thank you so much!
Give the full path of your file here
$location = "var/www/tmp/";
I think it will work. If its ok then store your servername in a variable and pass there.
Your Location should be like this:
// document root will give you the server root then you can add any directory after that (in your case its tmp I guess)
$location = $_SERVER['DOCUMENT_ROOT'] . '/your_preferred_dir/'
note: when you mention your preferred location you will have to make
sure that this location should exists otherwise it will cause error.
And not hardcoded as yours because it can change from server to server.
Hope this helps...

PHP file database upload

don't have much experience with php programming and web in general, so I've build this code from tutorials and what it supposed to do is to store data in database, but it seems like it completely ignores any statement(e.g. exception when choosing incorrect data format) and it sort of just refreshes the page after hiting submit button, any ideas how to fix this?
<div id='box'>
<form method='post' enctype='multipart/form-data'>
<?php
if(isset($_FILES['video'])){
$name = $_FILES['video']['name'];
$type = explode('.', $name);
$type = end($type);
$size = $_FILES['video']['size'];
$randon_name = rand();
$tmp = $_FILES['video']['tmp_name'];
if($type != 'mp4' && $type != 'MP4' && $type != 'flv' && $type !='avi'){
$message = "bad format";
}
else{
mysql_query("INSERT INTO videos VALUES('', '$name', 'videos/$randon_name.$type')");
$message = "successful upload";
}
echo $message;
}
?>
Choose file : <br/>
<input type='file' name='video'/>
<br/><br/>
<input type='submit' value='Upload'/>
</form>
</div>
It seems to be the case that you selected a file larger than your upload_max_filesize or post_max_size.
If this is the case then the $_FILES variable is empty and you see nothing. If you select a smaller file you should see something when doing a print_r($_FILES) at the top of your script.
if you run php in apache as a module you can try to add a .htaccess like this:
php_value upload_max_filesize 200M
php_value post_max_size 200M
Otherwise you need to edit your php.ini. As far as i know you cannot set these php.ini values via ini_set().

Max_File_Uploads directive php.ini

Im trying to do a simple file upload. I've done it many times before and it's been fine. For some reason this time I keep getting error UPLOAD_ERR_INI_SIZE coming up. Even though i've uploaded bigger files on the same server before. Here is my PHP.INI:
display_errors = On
short_open_tag = On
memory_limit = 32M
date.timezone = Europe/Paris
upload_max_filesize = 10M
post_max_size = 10M
And my HTML form:
<form action="/settings/upload-image" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="<?=(1024*1024*1024);?>">
<input name="files[]" id="attachfile" type="file" />
<br /><br />
<input type="submit" class="submit" value="Upload New Profile Image">
</form>
And my code:
foreach($files as $file)
{ $ext = strtolower(pathinfo($file[0], PATHINFO_EXTENSION));
if(in_array($ext,$allowed_upload_ext)===TRUE)
{
if(!$file[3]) { // If no error code
//$newFile = $me['id'].".$ext";
$newFile = $file[0];
resizeImage($file[2],PROFILE_IMAGES."/".$newFile,$ext,500);
genThumbFile($file[2],PROFILE_IMAGES."/thumb/".$newFile);
runSQL("UPDATE `users` SET `image`='{$file[0]}' WHERE `id`='{$me['id']}';");
array_push($msgs,"Image uploaded successfully.");
$me = select("SELECT * FROM `users` WHERE `id`='{$me['id']}';",true);
} else {
array_push($msgs,"!".fileError($file[3]));
}
} else {
array_push($msgs,"!The file ".$file[0]." could not be uploaded as it is the wrong file type.");
}
}
The only difference this time is that I am resizing and genorating thumbs with the temporary upload file instead of copying over the file first. Could that be the problem? I dont think so, because if the image is small it works perfectly fine. But I try anything like 2mb and it throws a fit.
Suggestions?
Thanks for ALL YOUR HELP guys. :P
I solved it - Missing line in PHP.INI:
file_uploads = On
Just for anyone who fins this.

Categories