PHP-newbie here
I'm trying to make a simple script to upload a photo to my data-base.
I know it's not very efective, but I want to do it anyway.
So I have these two .php files:
<html>
<body>
<form action="upload.php">
<input type="file" name="photo">
<input type="submit" name="submit" value="Submit" />
</form>
</body>
and
<?php
error_reporting( error_reporting() & ~E_NOTICE );
$conn = mysqli_connect("localhost", "root", "", "poze");
if(!$conn){
die("Connection failed: ".mysqli_connect_error());
}
else echo "Succes";
$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
//$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
//$imageType = mysql_real_escape_string($_FILES["image"]["type"]);
if($imageName==NULL){
echo "Null";
}
else{
echo "The photo is okay!";
}
After uploading any kind of picture, it echoes out as empty.
Could, anyone explain me why? And how could I solve it?
first you must use this form element:
<form enctype="multipart/form-data" action="upload.php" method="POST">
and secondly you must read file content from temporary folder and save to DB:
$tmpName = $_FILES['userfile']['tmp_name'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
then save this content to a field in mysql of type BLOB.
PHP-newbie here
I'm trying to make a simple script to upload a photo to my data-base.
I know it's not very efective, but I want to do it anyway.
So I have these two .php files:
<html>
<body>
<form action="upload.php">
<input type="file" name="photo">
<input type="submit" name="submit" value="Submit" />
</form>
</body>
and
<?php
error_reporting( error_reporting() & ~E_NOTICE );
$conn = mysqli_connect("localhost", "root", "", "poze");
if(!$conn){
die("Connection failed: ".mysqli_connect_error());
}
else echo "Succes";
$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
//$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
//$imageType = mysql_real_escape_string($_FILES["image"]["type"]);
if($imageName==NULL){
echo "Null";
}
else{
echo "The photo is okay!";
}
After uploading any kind of picture, it echoes out as empty.
Could, anyone explain me why? And how could I solve it?
SOLVING:
I changed the APIs so that I would only use mysqli
Index:
<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="file" name="photo">
<input type="submit" name="submit" value="Submit" />
</form>
</body>
PHP script:
<?php
error_reporting( error_reporting() & ~E_NOTICE );
$conn = mysqli_connect("localhost", "root", "", "poze");
if(!$conn){
die("Connection failed: ".mysqli_connect_error());
}
else echo "Succes"."<br>";
$imageName = mysqli_real_escape_string($conn ,$_FILES["photo"]["name"]);
$imageData = mysqli_real_escape_string($conn, file_get_contents($_FILES["photo"]["tmp_name"]));
$imageType = mysqli_real_escape_string($conn, $_FILES["photo"]["type"]);
//process the image ...
Related
I have a home FTP server. I made a little file uploader for that.
index.php:
<html>
<body>
<form enctype="multipart/form-data" action="upload_file.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
upload_file.php:
<?php
$ftp_server = "192.168.0.11";
$ftp_username = "";
$ftp_password = "";
// setup of connection
$conn_id = ftp_connect($ftp_server) or die("Pogreska tijekom spajanja.");
// login
if (#ftp_login($conn_id, $ftp_username, $ftp_password))
{
echo "Spojen kao $ftp_username#$ftp_server\n";
}
else
{
echo "Pogreska tijekom spajanja $ftp_username\n";
}
$file = $_FILES["uploadedfile"]["name"];
$remote_file_path = "/KUCNI_FTP-506D-ADD5/".$file;
ftp_put($conn_id, $remote_file_path, $_FILES["uploadedfile"]["tmp_name"], FTP_ASCII);
echo "\n\nconnection closed";
?>
And I get this error:
Warning: ftp_put(): Filename cannot be empty in C:\xampp\htdocs\ftp\upload_file.php on line 22
I tried to fix it, but it is not working. Please help me.
EDIT(And sorry for a little bit of Croatian...)
Have a look at MAX_FILE_SIZE, that's an option which is often ignored.
Make sure the file you're trying to upload is within the limits of what you previously defined.
I am trying to upload an image by updating MySQL table column with the image location.
<?php
session_start();
if(isset($_POST['upload']))
{
$link = mysqli_connect("localhost", "root", "root", "rental");
if($link == false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$image = $_FILES['propic']['tmp_name'];
$propic = addslashes(file_get_contents($image));
$lipic = "img/user.png";
$aadhar="img/user.png";
$sql= "UPDATE profiles SET profilepic='$propic', license='$lipic', aadhar='$aadhar' WHERE email='".$_SESSION['email']."'";
$result=mysqli_query($link,$sql);
}
?>
HTML Code:
<form id="form" method="post" action="profile.php" enctype="multipart/form-data">
<input type="file" name="propic" accept="image/*">
<input type="file" name="lipic" accept="image/*">
<input type="file" name="aapic" accept="image/*">
<input type="submit" class="mybutton myfont" name="upload" value="Submit" style="width:100px; background-color:black;">
</form>
MySQL table
Try this code. You are missing file uploading code.
$image = $_FILES['propic']['tmp_name'];
$name = "img/$_FILES['propic']['name']";
move_uploaded_file($image, $name); //Here you have to upload a file in a folder.
This is just for a reference. Use this and you will be able to upload a file on server.
I want to update the default profile picture
MY form is
<div class="profile-photo-container">
<img src="images/profile-photo.jpg" alt="Profile Photo" class="img-responsive" />
</div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=post enctype="multipart/form-data" >
<input type=hidden name="MAX_FILE_SIZE" value="1000000" />
<input type=submit value=submit />
</form>
And display code is
upload code is from this link
I do not see any php code but here is a code I use: please pay attention this is very basic, check security, etc.
Create a form:
<form action="handler.php" method='post' enctype="multipart/form-data">
Profile Description : <input type="text" name="description"/>
<input type="file" name="profile" value="profile" accept="image/png"/>
<input type="submit" name="submit" value="Upload"/>
</form>
handler.php
$name= $_FILES['profile']['name'];
$tmp_name= $_FILES['profile']['tmp_name'];
$submitbutton= $_POST['submit'];
$position= strpos($profile, ".");
$fileextension= substr($name, $position + 1);
$fileextension= strtolower($fileextension);
$description=$_POST['description'];
if (isset($name)) {
$path= 'path/to-your-upload/folder/';
if (!empty($name)){
if (move_uploaded_file($tmp_name, $path.$name)) {
echo 'Uploaded to Server !';
//connection to database
$conn= mysqli_connect($host, $user, $password);
if (!$conn)
{
die ('Could not connect:' . mysqli_error($conn));
}
mysqli_select_db($conn, $dbase);
if(!empty($description)){
mysqli_query($conn, "UPDATE $table SET description= '".$description."', profile_pic='".$name."' WHERE user_id='".$_SESSION['user_id']."' AND user_email='".$_SESSION['user_email']."' ");
}
mysqli_close($conn);
Still a newb to coding . have almost no idea what im doing . i tried to make a php page which would let me upload and view an image . do not know what is wrong . i tried to do it as correctly as possible . could someone please help me out ?
<!doctype html>
<html>
<head>
</head>
<body>
<form method="post">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
<?php
if(isset($_POST['submit']))
echo "button has been clicked";
$con = mysqli_connect("127.0.0.1","root","","demo");
if(!$con)
echo "didnt connect to database ";
else echo "connected ";
$imagename= mysqli_real_escape_string($_FILES['image'] ['name']);
$imagefile =mysqli_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
$qry = "INSERT INTO image (name,file) VALUES ('$imagename','$imagefile')";
$result = mysqli_query($con,$qry);
if($result)
echo "image has been uploaded";
viewimage();
function viewimage()
{$recon = mysqli_connect("127.0.0.1","root","","demo");
$view = "SELECT * FROM image ";
$data =mysqli_query($recon,$view);
$res2 =mysqli_fetch_assoc($data);
$currimage =$res2['file'];
echo "$currimage <br/>";
}
?>
</body>
</html>
To be able to catch a post variable, you need to submit the form and handle the action. The first problem with your code is that your form is not complete - it's missing a closing tag. Second thing, to be able to send a file through the post, you'll need multipart form. You should add enctype="multipart/form-data" as an attribute of the form.
So, instead of
<form method="post">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
You'll need
<form method="post" enctype="multipart/form-data">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
</form>
you must move the uploaded image to server
try this code -- create directory /uploads/
<!doctype html>
<html>
<head>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
<?php
if(isset($_POST['submit']))
echo "button has been clicked";
$con = mysqli_connect("127.0.0.1","root","","demo");
if(!$con)
echo "didnt connect to database ";
else echo "connected";
$uploads_dir = '/uploads';
$tmp_name = $_FILES["image"]["tmp_name"];
$name = $_FILES["image"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
$qry = "INSERT INTO image (name,file) VALUES ('$name','$tmp_name')";
$result = mysqli_query($con,$qry);
if($result)
echo "image has been uploaded";
viewimage();
function viewimage()
{$recon = mysqli_connect("127.0.0.1","root","","demo");
$view = "SELECT * FROM image ";
$data =mysqli_query($recon,$view);
$res2 =mysqli_fetch_assoc($data);
$currimage =$res2['file'];
echo '<img src="'.$currimage.'" /> <br/>';
}
?>
</body>
</html>
i tried to upload an image but when getimagesize the image is empty it is returning false.. and warning is coming ,it is not saving in database
.the database name is project and table name is images and fields are name and image .Here is a code...
<?php
ini_set('mysql.connect_timeout',300);
ini_set('default_socket_timeout',300);
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/formdata">
<input type="file" name="image"><br><br>
<input type="submit" name="submit" value="upload">
</form>
<?php
// Create connection
$conn = mysql_connect('localhost', 'root', '');
// Check connection
if (mysqli_connect_error()) {
die("Database connection failed: " . mysqli_connect_error());
}
else
{
echo "Connected successfully";
}
//data upload
if( isset($_POST['submit'] ))
{
if(getimagesize($_FILES['image']['tmp_name'])==FALSE) //image size is checked
{
echo "upload image";
}
else
{
$image= addslashes($_FILES['image']['tmp_name']);
$name=addslashes($_FILES['image']['name']);
$image=file_get_contents($image);
$image= base64_encode($image);
saveimage($name,$image);
}
}
function saveimage($name,$image)
{
$conn = mysql_connect('localhost', 'root','');
mysql_select_db("project",$conn);
$result = mysql_query("insert into images(name,image) values('$name','$image')"); //query implemented
}
?> //function written to save image
</body>
</html>
Edit this line
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<?php
ini_set('mysql.connect_timeout',300);
ini_set('default_socket_timeout',300);
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="image"><br><br>
<input type="submit" name="submit" value="upload">
</form>
<?php
// Create connection
$conn = mysql_connect('localhost', 'root', '');
// Check connection
if (mysqli_connect_error()) {
die("Database connection failed: " . mysqli_connect_error());
}
else
{
echo "Connected successfully";
}
//data upload
if( isset($_POST['submit'] ))
{
mysql_select_db("project",$conn);
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$name=addslashes($_FILES['image']['name']);
$result = mysql_query("insert into images(name,image) values('.$name.','.$image.')");
mysql_close($conn);
}?>
</body>
</html>