noob programer here. I'm doing a form and my teacher asked me to upload an image from any location of my computer and save it in a specific directory(also of my commputer). The code that i have only works if the image is in the same location as my script. Hope you can help me.
Note: Also i have to save it in the database because i'm going to use it to make a pdf.
HTML:
<html>
<head>
</head>
<form method="post" action="IConductores.php" >
<p></p>
<label>Foto</label>
<input type="file" id="Foto" name="Foto">
<input type="submit" id="Enviar" name="Enviar">
</form>
</html>
IConductores.php:
<?php
$Rfc = $_POST['Rfc'];
$Foto = $_POST['Foto'];
$SQL = "INSERT INTO Conductores VALUES ('$Rfc', '$Foto');";
$destdir = 'ImagenesPerfil/'; // path destination
$img=file_get_contents($Foto);
file_put_contents($destdir.substr($Foto, strrpos($Foto,'/')), $img);
add enctype="multipart/form-data" in your form tag
<form method="post" action="IConductores.php" enctype="multipart/form-data">
Uploaded file's name will be in $_FILES["Foto"]["name"]
And uploaded file location will be $_FILES["Foto"]["tmp_name"]
You need to use move_uploaded_file function like this
move_uploaded_file($_FILES["Foto"]["tmp_name"], $destdir.$_FILES["Foto"]["name"])
Related
Here is my code:
<html>
<body>
<div>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<input type="file" id="myFile" name="filename">
<input type="submit" name="Submit">
</form>
</div>
<?php
if(isset($_POST['Submit']))
{
$file = $_POST["myFile"];
echo("hello");
}
?>
</body>
</html>
when I run it and input a file it says: Undefined array key "myFile"
what am I doing wrong?
Uploaded files don't get added to the $_POST array but they are loaded into the $_FILES array. Try check that out.
You can then use the move_uploaded_file() to save it wherever you want.
Important: as soon as the script execution is over, the temporary uploaded file is removed if it was not saved somewhere else with move_uploaded_file().
The request transfers the data using the names of the form fields and not the IDs, the file data can be found in the global variable $_FILES.
Use $file = $_FILES['filename']['name']; for the name of the uploaded file
or $file = $_FILES['filename']['tmp_name']; for the temporary file path of the uploaded file.
Add on form enctype="multipart/form-data"
This value is necessary if the user will upload a file through the form.
Your file type name is "filename".
<html>
<body>
<div>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
<input type="file" id="myFile" name="filename">
<input type="submit" name="Submit">
</form>
</div>
<?php
if(isset($_POST['Submit']))
{
$file = $_FILES['filename']['tmp_name'];
echo("hello");
}
?>
</body>
</html>
I'm trying to build a file upload form and I'm having trouble with the very basics. My form is this:
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit"/>
</form>
</body>
</html>
My php code so far is one line and it doesn't do anything:
<?php
echo $_POST['filename'];
?>
The idea (at this point) is just to display the name of the file entered in the form. What am I doing wrong?
Based on your code I modified it. Have a try it.
HTML Part
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit" name="submit" />
</form>
</body>
</html>
PHP
if (isset($_POST['submit'])) {
// Check if files array is not empty
if (!empty($_FILES)) {
$imageName = $_FILES['filename']['name'];
echo $imageName;
// Insert your code related to upload
}
}
You can print the filename using the following code:
<?php
echo $_FILES["filename"]["name"];
?>
I have a simple HTML file with a form to upload a file and PHP code to process the uploaded file. I've tried a bunch of input types and they all work except for input type="file". Nothing gets displayed. The code is attached.
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload:
<input type="file" name="test.f" id="test.f">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
The processing php code is:
<?php
echo "Hello world";
echo $_FILES["test.f"];
?>
you should call the file by name if it does not work please change the file name in your form
name="test"
$_FILES["test"]['name'];
Here is my HTML Code
<HTML>
<HEAD>
<TITLE>Upload a File</TITLE>
</HEAD>
<BODY>
<H1>Upload a File</H1>
<FORM METHOD="POST" ACTION="PHP3.php">
<strong>File to Upload:</strong><br>
<INPUT TYPE="file" NAME="txt1" SIZE="50">
<P><INPUT TYPE="submit" NAME="submit" VALUE="Upload File"></P>
</FORM>
</BODY>
</HTML>
And here is my PHP Code
if ($_FILES['txt1'] != '')
{
mkdir("C:/xampp/CIS64/"); //Creates the CIS64 directory
$filename = "C:/xampp/CIS64/"; //Location of where the file will be
copy($_FILES['txt1']['tmp_name'], $filename.$_FILES['txt1']['name']) or die("Couldn't copy the file."); //Copies the uploaded file to the CIS64 directory
}
else
{
die("No input file specified"); //If the file doesn't open, close the program.
}
For some reason I get the error: "Undefined index: txt1 in C:\xampp\htdocs\PHP3.php on line 11"
It was working before and all of a sudden it stopped working. What is wrong with my code?
Check with
if (!empty($_FILES['txt1']))
because at the time when form is not posted there would be nothing like $_POST['txt1']
and <FORM METHOD="POST" ACTION="PHP3.php"> should be <FORM METHOD="POST" ACTION="PHP3.php" enctype="multipart-formdata"> for file uploading.
form must have attribute 'enctype=multipart/form-data'
Example: <form action=PHP3.php method=post enctype=multipart/form-data>
add strings to your "PHP3.php" such as var_dump($_POST); and var_dump($_FILES); and check what is inside - helps you to debug!
Location to save I set using $location = $_SERVER['DOCUMENT_ROOT'] . '/myuploaddir/' . $filename;
I am trying to upload a small mp3 through a simple html form using PHP.
Here is my html:
<html>
<head></head>
<body>
<form action="upload_mp3.php" method="post" enctype="multipart/form-data">
<input type="file" name="mp3" />
<input type="submit"/>
</form>
</body>
</html>
Here is my php:
<?php
$mp3 = ($_FILES['mp3']['name']);
$target = "mp3/";
$target = $target . basename( $_FILES['mp3']['name']);
if(move_uploaded_file($_FILES['mp3']['tmp_name'], $target))
{ header("Status: 200"); }
else
{ echo "no";}
?>
Does anything look wrong with the code. Also could it be that my temp file isn't writable? If so can I get some instructions on how to make it writable.
Thanks!
Make sure your form tag has the enctype=multipart/form-data attribute set:
<form action="upload_mp3.php" method="POST" enctype="multipart/form-data">