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')");
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:
I am trying to use simple php script to upload an image
The code below :
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
<title>Upload your files</title>
</head>
<body>
<form enctype="multipart/form-data" action="uploadfile.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?php
//echo exec('whoami');
//die($_FILES['uploaded_file']);
if(!empty($_FILES['uploaded_file']))
{
$path = "./img/backend/uploads/enquiry/";
$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!";
}
}
?>
I'm using php 7.1 I've already checked the folder for permissions. But when I try to submit it take me to some page with #custom kind of stuff in url. but if I try to post it without image it works fine.
What is the problem ? I think about apache which is not handle that properly. On my other server its working fine.
Just on this server I use auto ssl, is that the problem ?
See the image it take me like this link looks like its not submitting or rejected by server
Please change the path to this
$path = __DIR__."/img/backend/uploads/enquiry/";
You are missing a dot in the file path. Try the following:
$path = "./img/backend/uploads/enquiry/";
Full code:
<!DOCTYPE html>
<html>
<head>
<title>Upload your files</title>
</head>
<body>
<form enctype="multipart/form-data" action="#" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?php
if(!empty($_FILES['uploaded_file'])){
$path = "./img/backend/uploads/enquiry/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
echo $path;
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!";
}
}
?>
Without dot the file path will take script name as well, for example, if your PHP script/file name is "test.php", then without a leading dot in file path will create a path like (www is the folder where your test.php file is saved):
/www/test.php/img/backend/uploads/enquiry/
And hence it will be an invalid file path, with a leading dot it will skip it (layman terms) and your path will be like:
/www/img/backend/uploads/enquiry/
And this path is a valid one, hence it will upload.
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'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.
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">