Upload file trouble in PHP - php

I have made an upload image a couple times before and I never have problems. And I just copy the script and it worked fine, but after I copy I don't know why it keep shows that I'm not using the right extension. Can you spot the mistake?
This is the form script:
<tr height="30">
<td> </td>
<td align="right">Gambar Barang </td>
<td><input type="file" name="gambar" /></td>
</tr>
and its the action,
$errors= array();
$uploaddir = 'images/';
$uploadfile = $uploaddir . basename($_FILES['gambar']['name']);
$gambar = $_FILES['gambar']['name'];
$gambar_size = $_FILES['gambar']['size'];
$gambar_type = $_FILES['gambar']['type'];
$gambar_tmp = $_FILES['gambar']['tmp_name'];
$gambar_ext=strtolower(end(explode('.',$_FILES['gambar']['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($gambar_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($gambar_size > 4097152){
$errors[]='File size must be excately 4 MB';
}
if(empty($errors)==true){
copy($_FILES['gambar']['tmp_name'], $uploadfile);
}else{
print_r($errors);
}

There are several things that could cause file upload to fail:
does the upload form have enctype="multipart/form-data"?
is file_uploads = On in php.ini?
what is the max post/upload limit in php.ini?
did you check the permissions for the upload folder?

Your code works fine, just make sure your form uses:
method='post'
and(as already mentioned by Reto) - enctype="multipart/form-data"
i.e. something along the lines of:
<form method='post' action='#' enctype='multipart/form-data'>

Related

How to make a button, upload image to c9 folder?

I was wondering if it was possible to make a button in an HTML page or PHP that has the function to upload an image to your c9 folder instead "img" to your database(phpMyAdmin).
Once it is uploaded you can also display the image you just uploaded. But i think I can sort this one out like:
<html>
<img src='photo/'/>
</html>
//or
<?php echo "<img src='photo/'/>"?>
What I want to do with this is like filling a form in and have a photo with it as extra information.
edit:
I tried this code:
$target_dir = "photo/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
Once i clicked the button to upload it, it did not save the file in the folder.
edit:
It works now, had to do the php.ini setup in c9.
P.S: I just started programming and would like to learn. If I make my question unclear please tell me how I can improve my question to be more accurate. :D
Refer this and this is a simple question,before ask the this type of question please search and study from internet.
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>

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.

How to upload images to a domain at webhost using php?

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

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

PHP - File Upload spitting out error

I was working on a simple file upload exercise i've gotten from my WebDev Class - We actually only had to copy the code and integrate it to fit our needs.
I tried to do so for my project, sadly it will keep on echoing out the same error.
<?php
$allowed_filetypes = array('.jpg','.gif','.bmp','.png', '.jpeg');
$max_filesize = 524288;
$upload_path = 'uploads/';
$filename = $_FILES['userfile']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
var_export($_FILES, $ext);
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
if(filesize($_FILES['usrfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');
if(move_uploaded_file($_FILES['usrfile']['tmp_name'],$upload_path . $filename))
echo 'Your file upload was successful, view the file here';
else
echo 'There was an error during the file upload. Please try again.';
?>
It keeps on giving me the 'wrong filetype' error, with all of defined types in the array.
<form id='upload' action="uploadfile.php" method="POST" enctype="multipart/form-data">
<table>
<tr>
<td >Choose a File to Upload</td>
</tr>
<tr>
<td >Select File</td>
<td ><input type="file" name="userfile"></td>
</tr>
<tr>
<td colspan=2 id="sub"><input type="submit" name="submit" value="submit" ></td>
</tr>
</Table>
</form>
$filename = $_FILES['usrfile']['name'];
The 'tmp_name' index contains a temporary file name, not the one the file really had. That's stored in 'name'. See this page for information.
Additionally, you should:
check for errors in the 'error' index.
use pathinfo to get the extension,
lowercase it before searching the array,
add some randomness to the uploaded file name to avoid overwriting existing files.
You can use this as firstly you have to always echo the $_FILES array then start debuging.
change this line from
$allowed_filetypes = array('.jpg','.gif','.bmp','.png', '.jpeg');
to
$allowed_filetypes = array('jpg','gif','bmp','png', 'jpeg');
change the follwing line
if(filesize($_FILES['usrfile']['tmp_name']) > $max_filesize)
to
if(filesize($_FILES['usrfile']['size']) > $max_filesize)
and remove the line
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
and change this line
if(!in_array($ext,$allowed_filetypes))
to
if(!in_array($_FILES['usrfile']['type'],$allowed_filetypes))

Categories