upload image file to server using php - php

I am trying to upload selected image file into my ftp server on 1and1. I am not able to upload the file.
I have created folder called "uploadimages".
I have created a html form which has the following:
<form action="add.php" method="POST" enctype="multipart/form-data">
<div class="logo">
<label for="logoname" class="styled">Upload Logo (jpg / png):</label>
<div class="logofield">
<input type="file" id="logo" name="logo" size="30"/>
</div>
</div>
<input type="submit" value="Upload" name="submit" id="submit"/>
</form>
I have also create a php file which has the following:
<?php
$imagepic = $_FILES["logo"]["name"];
echo $imagepic;
$tempimgloc = $_FILES["logo"]["tmp_name"];
echo $tempimgloc;
$errorimg = $_FILES["logo"]["error"];
echo $errorimg;
if($errorimg > 0)
{
echo "<strong> <font size='18'>There was a problem uploading your Logo. Please try again!</font></strong>";
echo "<BR>";
}
else
{
move_uploaded_file($tempimgloc, "uploadimages/".$imagepic);
}
?>
ECHO printed results are:
1. Filename = testimage.png
2. Temp directory = /tmp/phpHvewUP
3. Error = 0
But they are not getting uploaded..
Where could I go wrong here?
Let me know!

You need to give access to your folder. Try this, chmod 777 uploadimages.

chmod 777 uploadimages
WRITING PERMISSION was the issue. Thanks Shapeshifter!

Related

Images not uploading to server path PHP

I have the directory photo_gallery (with 777 permissions) created in the same level as the Noticias.php file
It gives me no error, but the file is not showing in the photo_gallery folder.
This is my PHP code:
$error=array();
$extension=array("jpeg","jpg","png","gif");
foreach($_FILES["imagenes"]["tmp_name"] as $key=>$tmp_name)
{
$file_name=$_FILES["imagenes"]["name"][$key];
$file_tmp=$_FILES["imagenes"]["tmp_name"][$key];
$ext=pathinfo($file_name,PATHINFO_EXTENSION);
if(in_array($ext,$extension))
{
if(!file_exists("photo_gallery/".$file_name))
{
move_uploaded_file($file_tmp=$_FILES["imagenes"]["tmp_name"][$key],"photo_gallery/".$file_name);
}
else
{
$filename=basename($file_name,$ext);
$newFileName=$filename.time().".".$ext;
move_uploaded_file($file_tmp=$_FILES["imagenes"]["tmp_name"][$key],"photo_gallery/".$newFileName);
}
}
else
{
array_push($error,"$file_name, ");
}
}
And HTML:
<form action="Noticias.php" method="post" id="myform" enctype="multipart/form-data">
div class="form-group">
<p>Imágenes</p>
<input type="file" name="imagenes[]" class="form-control" id="imagenes" placeholder="Imágenes" multiple="multiple"/>
</div>
<button type="submit" class="btn btn-primary">Guardar</button>
</form>
move_uploaded_file() needs as second parameter the folder and the filename of the new location.
You should to do something like this:
move_uploaded_file($_FILES["imagenes"]["tmp_name"][$key], "photo_gallery/your_filename.png");
I hope that help you.
Regards.
give the file path as
./photo_gallery/
try liker this. This may help you.

move_uploaded_file not moving file with no error - searched other questions

I have searched all the questions of a similar nature yet have not been able to find the solution. I have checked my php.ini file and all permission and sizes are appropriate. The file is being created in mysql but I can not get a copy moved to my local folder. I have tried copy as well as setting permission with the commented out code seen below. Again, no errors are thrown that I can find.
Thank you. If you see the answer in another post feel free to supply a link.
<?php require_once("include/DB.php");?>
<?php require_once("include/Sessions.php");?>
<?php require_once("include/Functions.php");?>
<?php
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$target_file = ($_FILES["Image"]["name"]);
$Target =__DIR__."\Uploads\\".basename($_FILES["Image"]["name"]);
$Query="INSERT INTO admin_panel (image)
VALUES('$target_file')";
$Execute=mysqli_query($Connection, $Query);
chmod($target_dir,0755);
if(move_uploaded_file($_FILES['Image'],['tmp_name'], $Target)){
$_SESSION["SuccessMessage"]="uploaded ". $Target;
Redirect_to("testImage2.php");
}else{
$_SESSION["ErrorMessage"]="not uploaded ". $Target;
Redirect_to("testImage2.php");
}
if($Execute){
$_SESSION["SuccessMessage"]="Post Added Successfully";
Redirect_to("testImage2.php");
}else{
$_SESSION["ErrorMessage"]="Something went wrong failed to add";
Redirect_to("testImage2.php");
}
}
?>
<!DOCTYPE html>
<html>
<body>
<?php echo Message();
echo SuccessMessage();
?>
<form action="testImage2.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="Image" id="Image">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>

Using HTML POST to upload file via PHP

I am trying to upload a local file to webserver using HTML POST method and PHP.
this is my php code:
<?php
if (isset($_POST["submit"])) {
$updir = "/var/tmp/";
$upfile = $updir.basename($_FILES['rawexcel']['name']);
if(is_uploaded_file ($_FILES ["rawexcel"]["tmp_name"]))
{
move_uploaded_file ($_FILES["rawexcel"]["tmp_name"], $upfile);
} else {echo "error uploading file ".$upfile;}
} else {echo "not isset post method";}
?>
and HTML code is:
<div class="container" id="upl">
<h4> Upload files</h4>
<form action="upl.php" enctype="mutipart/form-data" method="post">
<p> upload your files to DB</p>
<p><input type="file" name="rawexcel" id ="rawexcel">
<input type ="submit" value="Upload" name ="submit"></p>
</form>
</div>
$_FILES["rawexcel"]["error"] shows 0 and from running this peice of code i get
error uploading file /var/tmp
I guess file name was not retrieved from html?
Error is in enctype:
enctype="multipart/form-data"
not:
enctype="mutipart/form-data"
You have typo mistake in enctype="multipart/form-data" , instead of this you typed enctype="mutipart/form-data" . So "mutipart" spelling is need to correct.

Upload limits on Heroku / Bucketeer (S3)

I am using Heroku and their S3 addon - "Bucketeer".
I have this working and I can upload images no problem. The thing is I don't care about uploading images. My app deals with uploading MP3s (nothing sinister - it's a web app for local bands).
I can't upload MP3s. It doesn't error out or anything, it's just that files don't end up in S3 - whereas images uploaded exactly the same way, do.
Editing to add - It's not a file type issue as I tested by uploading a very small mp3 file and this worked great.
Code below:
<form class="form" action="../model/process-track-upload.php" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-xs-12 col-sm-4">
<div class="dashboard-widget">
<h3>Add a song to your collection</h3>
<input class="input" type="text" name="trackname" placeholder="Track Name" required/>
<input class="input-margin" name="userfile" type="file">
<input type="hidden" name="userID" value="<?php echo $userID ?>" />
<button class="btn btn-large btn-block " type="submit" name="btn-upload">upload</button>
</div>
</div>
</form>
And the backend code:
require('../vendor/autoload.php');
$s3 = Aws\S3\S3Client::factory();
$bucket = getenv('S3_BUCKET')?: die('No "S3_BUCKET" config var in found in env!');
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['userfile']) && $_FILES['userfile']['error'] == UPLOAD_ERR_OK && is_uploaded_file($_FILES['userfile']['tmp_name'])) {
try {
$upload = $s3->upload($bucket, $_FILES['userfile']['name'], fopen($_FILES['userfile']['tmp_name'], 'rb'), 'public-read');
?>
<p>Upload successful :)</p>
<?php } catch(Exception $e) { ?>
<p>Upload error :(</p>
<?php } }
The answer to this question lay in default php settings.
HOWEVER as I was using Heroku, I didn't have access to the php.ini file.
So the answer was to create a .users.ini file which is like a user created override of the php.ini.
In that file I just set the following params:
upload_max_filesize = 15M
post_max_size = 15M
max_input_time = 0
LimitRequestBody 0
After this I was good to go!

php upload failure with move_upload_file()

I write the php to upload file and scan the directory to show them as links, the scaning directoy works well I can see the text files I created in the directory, but I just can not move the local file to the desired directory .No file shows up after execution.
I think the problem may contain in this line:
move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/BlueTapeLogin/upload".$_FILES["file"]["name"]);
What I really want is to upload the image to the directory in /var/www/BlueTapeLogin/upload
and my php file lives in /var/www/BlueTapeLogin/upload_image.php
How can I change the code to make things work? Thanks in advance.
Please see my full code:
<html>
<head>
<?php
try
{
if (!empty($_POST["delete"])){
$delete=$_POST["delete"];
echo"we have the command delete this file:";
echo $delete;
$file = "upload/".$delete;
echo "/n***************";
echo "you want delete :";
echo $file;
echo "***************";
if (!unlink($file))
{
echo ("Error deleting $file");
}
else
{
echo ("Deleted $file");
}
}else{}
}catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
?>
<?php
move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/BlueTapeLogin/upload".$_FILES["file"]["name"]);
?>
<?php
$dir=scandir("/var/www/BlueTapeLogin/upload") ;
for($j=0;$j<count($dir);$j++){
echo $dir[$j];
echo"\n";
$target = $dir[$j]; // This is the file that already exists
$link = $dir[$j]; // This the filename that you want to link it to
echo "".$link."";
}
?>
</head>
<body>
<form action="upload_image.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit"><br>
<label for="file">Delete</label>
<input type="text" name="delete" id="delete"><br>
<input type="submit" name="submit" value="Submit">
</form>
logout
</body>
</html>
You're missing a directory separator between upload and the filename, it should be:
move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/BlueTapeLogin/upload/".$_FILES["file"]["name"]);
The permissions you show say that only root can write into that directory, and but the webserver is probably using a userid like www-user. You need to change the ownership of the directory to the webserver userid. This will have to be done by the server administrator.
More likely, there's another directory that the webserver is already allowed to write into. The server administrator should be able to tell you what directory to use.
Check user permission to upload in that directory and try to make it simple. Just simply upload a file to other directory see it is working or not than check your code go one by one step.
Check return values check whether a warning is issued
Return Values
Returns TRUE on success.
If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.
If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.

Categories