move_uploaded_file function does not work - php

Why move_uploaded_file function does not work I have created a form in which I will upload some audio to the server folder but "move_uploaded_file" does not move the file. I don't know where I am wrong can please anyone help me.
<html>
<head>
</head>
<body>
<form action="uploading.php" method="post">
<input type="file" name= "audioFile"/><br>
<input type="Submit" value="Upload" name="Save_audio"/>
</form>
</body>
</html>
My HTML Code
uploading.php code
<?php
if(isset($_POST['Save_audio']) && $_POST['Save_audio']=="Upload")
{
$dir='Uploads/';
$audio_path=$dir.basename($_FILES['audioFile']['name']);
if (move_uploaded_file($_FILES['audioFile']['tmp_name'], $audio_path))
{
echo 'Uploaded';
}
}
?>

Don't assume a PHP function "doesn't work". Debug. In this case your browser isn't sending the file to the server at all.
Your form element is missing the encoding:
<form action="uploading.php" method="post" enctype="multipart/form-data">
Without the enctype the default encoding is application/x-www-form-urlencoded which can't hold files.

Because you are missed enctype="multipart/form-data" in form
<form action="uploading.php" method="post" enctype="multipart/form-data">

Related

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

Uploading image using php results in accept-file.php was not found

Hi im trying to upload an image to a database using php but every time i press the submit button i get the error
accept-file.php was not found
i dont see anywhere in my code where it will be directing to that is there something im missing?
<?php
session_start();
$link = mysqli_connect("localhost","root","","pictureupload");
if(isset($_POST['submit'])){
$imagename=$_FILES["iamge"]["name"];
$imagetmp=addslashes (file_get_contents($_FILES['image']['tmp_name']));
$insert_image="INSERT INTO images VALUES('$imagetmp','$imagename')";
mysqli_query($link,$insert_image);
}
?>
<!DOCTYPE html>
<html>
<head>
<Title>HomePage</Title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form action="accept-file.php" method="post" enctype="multipart/form-data">
Your Image: <input type="file" name="image" size="25" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
change your form tag to
<form action="?"
Your form has "accept-file.php" set as action:
<form action="accept-file.php" method="post" enctype="multipart/form-data">
This is where the form data will be sent to via the set method (POST in this case) and using the given encoding type.
After you click on "submit" your browser will call this script, which in your case does not exist. Therefore your webserver will return the error message instead of handling the upload.
To make it work you need to change the action to the filename of your script, which you have posted above.

HTML form for file upload won't return file 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'];

insert image into mssql using php

I want to insert image into mssql providing option for user to select the image and insert into database.so i used the following code
<html>
<head>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
mmysql_connect("localhost","sample","welcome");
mysql_select_db("samples");
if (empty($_POST["frm"]))
{
}
else
{
$filename=trim($_REQUEST['slcfile']);
$datastring = file_get_contents($filename);
$data = unpack("H*hex", $datastring);
echo trim($_REQUEST['slcfile']);
mssql_query("insert into imageinserter values ( 0x".$data['hex'].",'.$filename.')");
}
}
?>
</head>
<body>
<form name="frm" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" >
select image :<input type="file" name="slcfile" /> <br>
<input type="submit" name="slcfile" value="addimage"/>
</form>
</body>
</html>
but it is saying error error at file_get_contents saying no such file or resource found.
Please help to solve the issue
Thanks in advance
use multipart/form-data in your form like this:
<form enctype="multipart/form-data" name="frm" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
and then read the uploaded file by its temp name
eg: $_FILES['uploadedfile']['tmp_name']
You need to allow
allow_url_fopen
in your php.ini config file. Some hosts disallow it for security

unable to make image upload optional in codeigniter

I have four images to upload. And all these four images are optional in my form.
I tried to make it optional but I could not solve my problem. Please check my code below and let me know any solution for it.
<?php
if(isset($_FILES))
{
if(empty($_FILES['testfile']['name']))
{
echo 'no';
}else{
// do the form validation here
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" enctype="multipart/form-data">
<input type="file" name="testfile"/>
<input type="submit" value="upload">
</form>
here is some test you can try i hope it helps

Categories