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
Related
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'>
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.
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.
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 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