I have just started to code in PHP and i have a problem with the code of the uploader: I use xampp and when I upload the file the result is
File uploaded in C:xampp\htdocs\myfile.php
but the file does not exist.
Code Snippet below:
<form action="sys.php" method="POST" ectype="multipart/form-data">
<c size=6>FILE TO UPLOAD</c><br>
<input type="file" name="sys"><br><br>
<input type="submit" value="UPLOAD" name="upload"><br><br>
<?php
$path = "C:xampp\htdocs";
if (isset($_POST["upload"])){
move_uploaded_file($_FILES["sys"]["tmp_name"], $path);
echo "File Uploaded in $path\YOURFILE";
}
else{
echo "[-]ERROR the uploader doesn't work";
}
?>
</html>
change double quote to single and also set name for uploaded image.example code are added below . Please put this inside if (isset($_POST["upload"])) condition
$path = 'C:\xampp\htdocs\\'.$_FILES["sys"]["name"];
Related
Hi there I'm trying to create an upload form in php. Containing, upload input, text input (name to be given to the file that was uploaded), and submit button. However I don't know much about php, so I don't know how to actually link that what I had typed on the <input type="text"/> becomes the name of the file when uploaded. If someone can help ? Thanks.
Here is my code:
<!DOCTYPE html>
<html>
<head>
<title>Tu peux uploader ici ta video.</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="text" name="file-name"></input><br /> <!-- [ASK]: How to make this the file name of the uploaded file -->
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?PHP
if(!empty($_FILES['uploaded_file']))
{
$path = "uploads/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
move_uploaded_file ( string $filename , string $destination ) takes filename which is the filename of the uploaded file and destination is the destination of the moved file. Note that destination directory must exist; move_uploaded_file() will not automatically create it. So lets get the name now...
$_FILES['uploaded_file']['tmp_name'] gives you the temporary filename of the file in which the uploaded file was stored on the server eg. C:\Users\USER\AppData\Local\Temp\phpD3C.tmp
while
$_FILES['uploaded_file']['name'] gives you the actual name which you need to extract the extension eg. myfile.jpg
To link that what you had typed on the , first get it via $_POST["file-name"], then concatenate with the extension. Use pathinfo to retrieve the original extension.
Change your code to become...
<!DOCTYPE html>
<html>
<head>
<title>Tu peux uploader ici ta video.</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="text" name="file-name"></input><br /> <!-- [ASK]: How to make this the file name of the uploaded file -->
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(!empty($_FILES['uploaded_file']))
{
$file_parts = pathinfo(basename( $_FILES['uploaded_file']['name'])); //access the actual name instead of tmp_name
//just pick the extension of the file_parts and concatenate it to your path
$path = 'images/';
$path = $path . $_POST["file-name"] . "." . $file_parts['extension'] ;
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename($path) ." has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
}
?>
This should get you the desired name. So if you upload file dog.php and in the text field you have cow, the resulting name should be cow.php.
Result:
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 ';
}
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>
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.
I'm trying to do uploading of image in the server and retrieving. I have done something but the problem is that the image is not uploading. My script will only get the file name and then the image will be save on the server folder. This script only saves the other data but not the image, I'm not sure where is the mistake, but the path I've used is corret, also I need to retrieve the image and post it on front end.
<div class="wrap">
<form method="POST">
Date: <input type="text" name="class_date"> <br>
Event: <input type="text" name="class_name"><br>
<input type="hidden" value="class_sched" name="posttitle"><br>
<input type="hidden" name="size" value="350000">
Image:<input type="file" name="class_image"><br>
<input type="submit" value="submit" name="submit">
</form>
</div>
<?php
global $wpdb;
if(isset($_POST['submit']))
{
$target = plugins_url("/images",__FILE__);
$target = $target . basename( $_FILES['class_image']['name']);
$class_date = $_POST['class_date'];
$class_name = $_POST['class_name'];
$class_image = ($_FILES['class_image']['name']);
$wpdb->insert('wp_calendar', array('class_name'=> $class_name, 'class_date'=>$class_date, 'class_image'=>$class_image));
//Writes the photo to the server
if(move_uploaded_file($_FILES['class_image']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['class_image']['name']). " has been uploaded, and your information has been added to the directory";
}
else
{
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
}
Add move_uploaded_file function. In your above code only check move_uploaded_file is true or not. Because it is if statement.
Add following code after $wpdb->insert('wp_calendar', ....
move_uploaded_file($_FILES['class_image']['tmp_name'];
move_uploaded_file function will move file from temporary folder to plugin/images folder.