Upload mp3 to Server PHP - php

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">

Related

Can't display image I uploaded in PHP

I'm trying to upload an image and display after uploading, the upload part works fine but image can't display.
Any answers?
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo <<<_END
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="fupload" size="100000" accept="image/*">
<input type="submit" name="upload" value="Upload">
</form>
_END;
if($_FILES){
$name = $_FILES['fupload']['name'];
move_uploaded_file($name = $_FILES['fupload']['tmp_name'], $name);
echo "<br><img src='$name'>";
}
?>
</body>
</html>
Browser:
Image can't display
nevermind, the problem is move_upload ($name=xxxx, $name), it means you assign to $name the tmp source !
here is a working code
<!DOCTYPE html>
<html>
<body>
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="fupload" size="100000" accept="image/*">
<input type="submit" name="upload" value="Upload">
</form>
<?php if($_FILES)
{
$source=$_FILES['fupload']['tmp_name'];
$target1 = $_FILES['fupload']['name'];
move_uploaded_file($source,$target1);
?>
<br>
source=<?php echo htmlspecialchars($source);?>
<br>
target=<?php echo htmlspecialchars($target1);?>
<img src="<?php echo htmlspecialchars($target1);?>"
<?php
} // if $_FILES
?>
</body>
</html>
Ok, following comment, it seems $name point To à path not accessible for external user. Try a link like this $name="c:\path To your base path\www\et.png"
Edit: supposing you have a existing www folder , where you find your index.php. It may be called public.

Trouble accessing HTML form data using PHP

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"];
?>

PHP and HTML Undefined Index on file 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;

php uploaded file not visible

After reading the w3schools file (http://www.w3schools.com/php/php_file_upload.asp) I followed the same steps to upload the image file. However i'm not able to see the file in wampp\tmp folder. Here is my code:
<?php
if($_FILES["file"]["error"] >0 )
{
echo "ERROR:" .$_FILES["file"]["error"]. "<br>";
}
else
{
echo "upload" .$_FILES["file"]["name"] . "<br>";
echo "type" .$_FILES["file"]["type"]. "<br>";
echo "Size" . $_FILES["file"]["size"]."<br>";
echo "stored in" .$_FILES["file"]["localhost/tom/upload/"]. "<br>";
}
?> <!DOCTYPE html>
<head>
<title>Uploading file</title>
</head>
<body>
<form name="f1" action="fup2.php" method="post" enctype="multipart/form-data">
<input type="file" name="f1" /><br />
<input type="submit" value="send" />
</form>
</body>
</html>
Only sessions are saved in the tmp folder, but not the image..
Please suggest.
You will have to move the file from the temp folder to an real one.
Try:
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
If you like following w3schools they explain it right under where you linked.
("Saving the Uploaded File")
Make sure the form has at least the following:
<form method="post" enctype="multipart/form-data">
Files can only be sent through POST, and the enctype needs to be multipart/form-data.
Check the PHP error log if that wasn't the issue.
You can find the location where temporary files are supposed to be saved with sys_get_temp_dir().

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