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