Images not uploading to server path PHP - 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.

Related

How to send file to $_FILE through html form?

Well pretty simple question.. But can't get it right for some reason.
What would be the html code to send a file to this?
move_uploaded_file($FILES["upload"]["tmpname"], $_POST["name"]);
Here's mine but when I used it and echo/vardump everything, I only have 'name' and not the file
<form action="uploader.php" method="post" id="myForm" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="upload" id="upload">
<input type="text" name="name" id="name">
<button name="submit" class="btn btn-primary" type="submit" value="submit">Upload File</button>
</form>
Thank you
i try to add a comment but i can't
first check if upload permission is on in php.ini
file_uploads = On
If it set to on check your upload directory which you added in uploader.php file and use if to check $_FILES['upload'] is empty
this is a simple code to uploader.php file
<?php
if(!empty($_FILES['upload']))
{
$path = "upload/"; /// directory to upload
$path = $path . basename( $_FILES['upload']['name']);
if(move_uploaded_file($_FILES['upload']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['upload']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}else {
echo 'No file selected to upload ';
}

PHP file upload does not work in XAMPP

I'm trying to upload an xml file using an Android app I developed.
To do this, I used a free hosting space, and it works great. But now, I want to upload files to one of my pc's folders, using xampp as a web server.
The problem is that when I try to upload files here something goes wrong.
I am sure my php code is good, because it works with the hosting service I have my website on, so I only changed the path, as you can see below.
For hosting service:
<?php
if (is_uploaded_file($_FILES['transactions']['tmp_name'])) {
$uploads_dir = '/membri/cendav/gestione_magazzino/ExportData/';
$tmp_name = $_FILES['transactions']['tmp_name'];
$pic_name = $_FILES['transactions']['name'];
move_uploaded_file($tmp_name, $uploads_dir.$pic_name);
}
else{
echo "File not uploaded successfully.";
}
?>
Now, the code for xampp, which is the same:
<?php
if (is_uploaded_file($_FILES['transactions']['tmp_name'])) {
$uploads_dir = 'exportdata/';
$tmp_name = $_FILES['transactions']['tmp_name'];
$pic_name = $_FILES['transactions']['name'];
move_uploaded_file($tmp_name, $uploads_dir.$pic_name);
}
else{
echo "File not uploaded successfully.";
}
?>
As you can see, I only changed the $uploads_dir. The path from where I access the file upload.php, from my application http://10.0.0.202:1024/Warepad/.
I also changed the permissions of the exportdata/ folder, to everyone, but it still doesn't work.
P.S. I know there's a ton of these issues, but I still can't find what the problem is.
try this:
<form action="fileupload.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" />
</form>
So, now it seems to work. It's strange, because the file upload.php is the same, and I haven't changed anything else!
?php
if (is_uploaded_file($_FILES['transactions']['tmp_name'])) {
$uploads_dir = 'exportdata/';
$tmp_name = $_FILES['transactions']['tmp_name'];
$pic_name = $_FILES['transactions']['name'];
move_uploaded_file($tmp_name, $uploads_dir.$pic_name);
}
else{
echo "File not uploaded successfully.";
}
?>
The Android app, now, is able to upload file correctly, but if I try to upload from an html form, it still doesn't work.
Here's the html code:
<html>
<head>
<title>Upload</title>
<meta charset="utf-8">
</head>
<body>
<form action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="30000">
Invia questo file: <input name="transactions" type="file">
<input type="submit" value="Invia File">
</form>
</body>
</html>

PHP 'Upload File' code not working

HTML in 'index.php':
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload" accept="application/pdf, .pdf" required><br>
<input type="submit" name="submit" value="SUBMIT">
</form>
PHP in 'index.php':
<?php
if (isset($_POST['submit'])) {
$target_dir = 'docs/';
$temp_loc = $_FILES['fileToUpload']['tmp_name'];
$file = $_FILES['fileToUpload']["name"];
if(move_uploaded_file($temp_loc,$target_dir.$file)) {
?><script>alert('successfully uploaded');</script><?php
} else {
?><script>alert('error while uploading file');</script><?php
}
}
?>
The following code doesn't seem to upload any of the documents I select to the target dircetory, that is, '/docs'.
I'm using wampserver with localhost, if that is needed at all. The 'file-upload' is allowed in the 'php.ini'
I've seen many tutorials and have even done troubleshooting on Stackoverflow itself, but I can't seem to make it work.
Thanks in advance

wrong use of class?

there occured a suspect problem during my image upload, i´ll show you:
<form action="check.php" method="POST">
File: <input type="file" name="picture" value="" id="picture-field">
<span class="error" id="file-error"></span>
<br><br>
<input type="submit" name="submit" value="send">
</form>
my check.php:
require 'ImageChecker.php';
if (!$_FILES)
echo "exit because no FILES";
$imageChecker = new ImageChecker();
$imageError = "";
if(!$imageChecker->php_error($_FILES['picture']['error'])) {
$imageError = "php error ocurred!";
echo $imageError;
the class ImageChecker.php:
class ImageChecker {
// function for php-error-check
public function php_error ($php_error) {
if ($php_error === UPLOAD_ERR_OK) {
return TRUE;
} else {
return FALSE;
}
}
// more functions..
}
every time, i upload an image, i get:
exit because no FILES
and
php error ocurred!
WHY ? what did i wrong? really need your help, thanks and greetings!!
In the form tag you need to give
enctype="multipart/form-data"
<form action="check.php" method="POST" enctype="multipart/form-data">
this is very important while uploading files

upload image file to server using 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!

Categories