PHP file upload does not work in XAMPP - php

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>

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 ';
}

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>

File not being uploaded php

UPDATE
I have now solved it, the problem was that I did not have the preciding '/' and I did not have the '/' at the end, here is the final syntax of the folder path: $folder = '/home/dl/www/uploads/';
UPDATE
I have updated the code so that is can output some more detailed debugging information, and this is what is output:
Warning: move_uploaded_file(home/dl/www/uploads/test.txt): failed to open stream: No such file or directory in /home/dl/public_html/file-upload/upload.php on line 9
I have the below script which is attempting to upload a selected file to a specified directory on my web server. However, it is not uploading; I have checked that the permissions on the directory is at '777' as per numerous tutorials have suggested, but it is still throwing the 'File is not uploaded' message.
Does anyone have any suggestions as to why this may not be working?
Thanks!
<?php
$folder = "/home/dl/www/uploads";
if (is_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'])) {
if (move_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'], $folder.$HTTP_POST_FILES['filename']['name'])) {
Echo "File uploaded";
} else {
Echo "File not moved to destination folder. Check permissions";
};
} else {
Echo "File is not uploaded.";
};
?>
The HTML form:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="upload.php" method="post"
enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
Try
$folder = "/home/dl/www/uploads/";
instead of
$folder = "/home/dl/www/uploads";
That is, a slash at the end.

File is not uploading giving error "File is not uploaded please try again"?

i'ma beginner and i'm trying to upload a file to uploads/ folder from this tutorial but as i run my program after submit it's showing error There was an error uploading the file, please try again! can you tell me what's wrong here??
form.php:
<html>
<head>
<title>Form image</title>
</head>
<body>
<form enctype="multipart/form-data" action="uploader.php" method="post" name="changer">
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
<input type="file" name="uploadedfile" accept="image/jpeg">
<input type="submit" value="Submit">
</form>
</body>
</html>
uploader.php
<?php
//where the file is going to be placed..
$target_path="uploads/";
$target_path=$target_path.basename($_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['name'], $target_path)){
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
The correct code is
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)
You are using
move_uploaded_file($_FILES['uploadedfile']['name'], $target_path)
This will not work since the file is stored in the temp folder after the upload is initiated and while moving the file it must know the temp name so that the file could be moved from temp folder to the server.

File Upload php

So I am trying to upload files to the database through php here is the code
HTML:
<!doctype html>
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<form action="uploadimage.php" method="POST" enctype="multipart/form-data">
<input type="text" name="tag"/>
<input type="file" name="file"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
php:
<?php
$target = "files/";
$target = $target . basename( $_FILES['file']['name']);
$name=$_POST['tag'];
$file=($_FILES['file']['name']);
mysql_connect("localhost","Bluesir9","Bluesir9","website");
mysql_query("Insert into files values('$name','$file');");
if(move_uploaded_file($_FILES['file']['tmp_name'],$target))
{
echo "The File ".basename($_FILES['file']['name'])."has been uploaded";
}
else
{
echo "Sorry there was an error";
}
?>
when I check the target folder the files are there, but I cant find the files on the database returns an empty result set when I query it through phpMyAdmin.
What am I doing wrong?
Since the target files are there in folder, and if you can't find the files on the database then obvious thing should be to check insert query.
Please check syntax of insert query
mysql_query("Insert into files values('$name','$file')");

Categories