I'm having an issue with uploading an image path into a database. Database called: basketball_database and table called: media, columns: id(int, auto increment), name(varchar), image(varchar)
All the images I try to upload from different directories on my computers are png files.
For some reason I'm only able to upload only 1 image and the database doesn't display the image name or the image path. Can anyone help me fix these problems? Thank you
File 1: upload_image.php
<html>
<body>
<form action="uploaded_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
File 2:upload_file.php
//connection
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "media";
$con = mysql_connect($hostname, $username, $password)
or die("Unable to connect to Mysql");
// echo "Connected to mysql<br>";
mysql_select_db("$database")
or die("Could not select Basketball_database");
//echo "Connected to database";
//image extensions allowed
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
//Successfully uploaded
echo "Your file " . $_FILES["file"]["name"] . " successfully uploaded!!<br>";
echo "Details :";
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
//Display Image
echo "<img src=uploaded/" . $_FILES["file"]["name"] . ">";
//Uploaded image folder
if (file_exists("uploaded/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploaded/" . $_FILES["file"]["name"]);
}
}
}
else
{
//error message on the extension that are not allowed
echo "Invalid file";
$filename = preg_replace('/[^A-Z0-9]/','',$_FILES["file"]["name"]) . $extension;
$logo = uploaded/$filename;
//insert into database
$strSQL = "INSERT INTO $table(name,image) VALUES('$name_file','$logo')";
mysql_query($strSQL) or die(mysql_error());
}
?>
Here is a good tutorial to start with upload img with php.
And just build up from here.
Related
I am trying to upload an image with my form data, but I am getting errors.
Inside my form, the lines for the file are:
<li id="li_6" >
<label class="description" for="file">Filename:</label>
<div>
<input type="file" name="file" id="file"><br>
</div><p class="guidelines" id="guide_5"><small>Select the image to upload.</small></p>
</li>
And here is the upload script:
if(isset($_POST['title'])) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 60000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
"media/uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file";
}
$img_url = '/media/uploads/'.$_FILES["file"]["name"];
$title = $_POST['title'];
$description = $_POST['description'];
$type = $_POST['description'];
$startPrice = $_POST['startPrice'];
$reservePrice = $_POST['reservePrice'];
$buyPrice = $_POST['buyPrice'];
not too sure whats going wrong. I've looked it over. I mean the code is directly from the manual.. it should work!
however, I am getting this error:
>
A PHP Error was encountered
Severity: Notice
Message: Undefined index: file
Filename: controllers/user.php
Line Number: 76
The most common reason for this error is forgetting to set the enctype parameter of the <form>:
<form method="post" action="..." enctype="multipart/form-data">
More info: Why File Upload didn't work without enctype?
I have some issues with my webpage. I'm creating an app for Android which will can send image to http address. So I took an example from w3schools for upload script. The problem I have is that when I try to upload file from my computer (the app is not ready yet) I have an error 405 Not Allowed and some sign "nginx". Can you help me fix the problem?
Here is my page:
<html>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
And the php script:
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file";
}
?>
I have numbers of image to upload to my database and i submit its value in one form..
i want to call this function to all my images to upload and insert them in my database in the same time...please i need help
<form method="post" action="insert.php" enctype="multipart/form-data">
<tr><td>pic</td>
<td><input type="file" name="file" /></td></tr>
<tr><th>card </th>
<td><input type="file" name="cc" /></td></tr>
<input type="submit" name="submit" value="submit" />
</form>
<?php
include("connect.php");
if(isset($_POST['submit']))
{
function imgs(){
var_dump($_FILES);
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("img/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
"img/" . $_FILES["file"]["name"]);
echo "Stored in: " . "img/" . $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file";
}
return $i="Stored in: " . "img/" . $_FILES["file"]["name"];}
$pic=$_FILES['file']['name'];
$pic=imgs($_FILES['file']['name']);
$sql1="insert into emp values ('', 'img\/".$pic."')";
$r1=mysql_query($sql1,$con);
if($r1){ echo "good";} else {echo "fail";}
$cc=imgs($_FILES['cc']['name']);
$sql2="insert into doc values ('','img\/".$cc."')";
$r2=mysql_query($sql2,$con);
?>
?>
Heading
html and php in to data base
body
<html>
<body>
<form action="insert.php" method="post">
File: <input type="text" name="file">
Filename: <input type="text" name="filename">
<input type="submit">
</form>
</body>
</html>
<?php
$con=mysqli_connect("example.com","acount","password","img");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//tip insert $_POST to recive $_GET
$File = mysqli_real_escape_string($con, $_POST['file']);
$FileName = mysqli_real_escape_string($con, $_POST['filename']);
//insertin in to data base
$sql="INSERT INTO Persons (file, filename)
VALUES ('$File', '$FileName')";
//chequing for erros on database
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
//close database
mysqli_close($con);
?>
I look at w3school tutorial for upload a file. I can understand the code. The first 2 simply version of the code works, while this doesn't.
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
I have a problem, if i use this code to upload a file i Always get Invalid file response.
EDIT:
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Also check Folder permission. Upload folder permission should be 777.
this code only uploads images.
you cannot upload same files that are already uploaded.
you cannot upload files more than 20KB.
make sure you have a directory named 'upload' in your source folder
Hi i have this code to upload files, and it works fine, but when i want to download files from the folder and they have acents the page doesn't download.
Example: /Prova%20de%20Aptidão%20Profissional.png
The requested URL /Prova de Aptidão Profissional.png was not found on this server.
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Try
urlencode($_FILES["file"]["name"]);
It would be better to filter out those characters & not putting spaces in folders tho.