So, you know how you make a image uploading system with InfinityFree PHP hosting?
Well, I made one but it has a problem. When I get the image and paste the link in like example Discord Chat, it does not show as a Image, it just shows the link. I saw somebody use InfinityFree and pasted the link, but it works! I don't know how to make happen. Please Help
Coding: HTML CODE
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
PHP code:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . uniqid("", false));
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
Anyone know how to help?
Code below works fine without PRG, BUT i want PRG because i really hate refresh-repost. Though, i did not foresee that problem: i assume that if i dont upload my images immediately with post, get wont find any files to move from temp folder to server folder (they are deleted immediately after post i guess?).
Now, since images should be uploaded before filtered anyway (using server sided languages), i had an idea of uploading without using PRG, and then using a session state like a temp file that will include all the submit info in order just to destroy it and get refresh-repost free... well, its not working. I know it uploads only the correct files, BUT i cant get any $vars out of it, resulting in being unable to show my $error variables / warnings for users.
So... how do i get two birds with one shot php gurus?
trying to figure out if 'fake' sessions can work:
<?php
session_start();
include_once('../includes/connection.php');
if (isset($_SESSION['logged_in'])) {
if (isset($_POST['submit_pic'])) {
$countfiles = count($_FILES['image']['name']);
$target_dir = "../images/";
$error_array = array();
$upload_array = array();
$eupload_array = array();
for($i=0;$i<$countfiles;$i++) {
$filename = $_FILES['image']['name'][$i];
$target_file = $target_dir.$filename;
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$check = getimagesize($_FILES["image"]["tmp_name"][$i]);
if($check !== false) {
$uploadOk = 1;
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
array_unshift($error_array, "Image $filename is not JPG, JPEG, PNG or GIF.");
$uploadOk = 0;
}
} else {
array_unshift($error_array, "File $filename is not an image.");
$uploadOk = 0;
}
if (file_exists($target_file)) {
array_unshift($error_array, "Image $filename already exists.");
$uploadOk = 0;
}
if ($_FILES["image"]["size"][$i] > 500000) {
$u = 1;
array_unshift($error_array, "Image $filename is too large.");
$uploadOk = 0;
}
if ($uploadOk == 0) {
array_unshift($eupload_array, "Sorry, image $filename was not uploaded.");
} else {
if (move_uploaded_file($_FILES['image']['tmp_name'][$i], $target_file)) {
array_unshift($upload_array, "Image $filename has been uploaded.");
} else {
array_unshift($eupload_array, "Sorry, $filename there was an error uploading your file.");
}
}
}
}
if(strcasecmp($_SERVER['REQUEST_METHOD'],"POST") === 0) {
$_SESSION['postdata'] = $_FILES;
header("Location: ".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
exit;
}
if( isset($_SESSION['postdata'])) {
$_FILES = $_SESSION['postdata'];
unset($_SESSION['postdata']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel='stylesheet' href='../assets/style.css'/>
<title>CMS TEST</title>
</head>
<body>
<div class='container'>
<a href='add_picture.php' id='logo'><h2>Add Picture</h2></a>
<br/>
<small class='test_warning' style='color:#B83333'>
❶ Images should be JPG, JPEG, PNG or GIF format extentions. <br/>
❷ Images should not be above 500KB size.<br/>
❸ Please try to rename images before uploading for easier use.
</small> <br/><br/>
<form action='add_picture.php' method="post" enctype="multipart/form-data">
<input type="file" name="image[]" id="image" class="custom-file-input" multiple required/><br/><br/><br/>
<input type="submit" value="Upload" name="submit_pic"/>
</form>
<?php if (!empty($upload_array)) { ?>
<small style='color:#00FF00'>
<?php echo '<br/><br/>', implode("<br/>",$upload_array), '<br/>'; ?>
</small>
<?php }?>
<?php if (!empty($eupload_array)) { ?>
<small style='color:#aa0000'>
<?php echo '<br/><br/>', implode("<br/>",$eupload_array), '<br/>'; ?>
</small>
<?php }?>
<?php if (!empty($error_array)) { ?>
<small style='color:#aa0000'>
<?php echo implode("<br/>",$error_array), '<br/>'; ?>
</small>
<?php }?>
<br/><br/>
<a href='index.php'>← Back</a>
<br/>
</div>
<a class='logout' href='logout.php'>Logout</a>
</body>
</html>
<?php
} else {
header('Location:index.php');
}
?>
'proper one':
<?php
session_start();
include_once('../includes/connection.php');
if (isset($_SESSION['logged_in'])) {
if( strcasecmp($_SERVER['REQUEST_METHOD'],"POST") === 0) {
$_SESSION['postdata'] = $_FILES;
header("Location: ".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
exit;
}
if( isset($_SESSION['postdata'])) {
$_FILES = $_SESSION['postdata'];
$countfiles = count($_FILES['image']['name']);
$target_dir = "../images/";
$error_array = array();
$upload_array = array();
$eupload_array = array();
for($i=0;$i<$countfiles;$i++) {
$filename = $_FILES['image']['name'][$i];
$target_file = $target_dir.$filename;
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$check = getimagesize($_FILES["image"]["tmp_name"][$i]);
if($check !== false) {
$uploadOk = 1;
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
array_unshift($error_array, "Image $filename is not JPG, JPEG, PNG or GIF.");
$uploadOk = 0;
}
} else {
array_unshift($error_array, "File $filename is not an image.");
$uploadOk = 0;
}
if (file_exists($target_file)) {
array_unshift($error_array, "Image $filename already exists.");
$uploadOk = 0;
}
if ($_FILES["image"]["size"][$i] > 500000) {
$u = 1;
array_unshift($error_array, "Image $filename is too large.");
$uploadOk = 0;
}
if ($uploadOk == 0) {
array_unshift($eupload_array, "Sorry, image $filename was not uploaded.");
} else {
if (move_uploaded_file($_FILES['image']['tmp_name'][$i], $target_file)) {
array_unshift($upload_array, "Image $filename has been uploaded.");
} else {
array_unshift($eupload_array, "Sorry, $filename there was an error uploading your file.");
}
}
}
unset($_SESSION['postdata']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel='stylesheet' href='../assets/style.css'/>
<title>CMS TEST</title>
</head>
<body>
<div class='container'>
<a href='add_picture.php' id='logo'><h2>Add Picture</h2></a>
<br/>
<small class='test_warning' style='color:#B83333'>
❶ Images should be JPG, JPEG, PNG or GIF format extentions. <br/>
❷ Images should not be above 500KB size.<br/>
❸ Please try to rename images before uploading for easier use.
</small> <br/><br/>
<form action='add_picture.php' method="post" enctype="multipart/form-data">
<input type="file" name="image[]" id="image" class="custom-file-input" multiple required/><br/><br/><br/>
<input type="submit" value="Upload" name="submit_pic"/>
</form>
<?php if (!empty($upload_array)) { ?>
<small style='color:#00FF00'>
<?php echo '<br/><br/>', implode("<br/>",$upload_array), '<br/>'; ?>
</small>
<?php }?>
<?php if (!empty($eupload_array)) { ?>
<small style='color:#aa0000'>
<?php echo '<br/><br/>', implode("<br/>",$eupload_array), '<br/>'; ?>
</small>
<?php }?>
<?php if (!empty($error_array)) { ?>
<small style='color:#aa0000'>
<?php echo implode("<br/>",$error_array), '<br/>'; ?>
</small>
<?php }?>
<br/><br/>
<a href='index.php'>← Back</a>
<br/>
</div>
<a class='logout' href='logout.php'>Logout</a>
</body>
</html>
<?php
} else {
header('Location:index.php');
}
?>
Besides temp pictures not existing between post get requests, also the variables are deleted from refreshing and i could not use them inside my html to show errors.
So i tried to play around with sessions even more, i moved the pic upload code to post, created some sessions to store my error arrays, and then at get i recreated my error arrays from the sessions i created for them.
Problem solved, pics are filtered, errors displaying, PRG is working.
Gosh... i just started with php there are soooo many things i dont understand D:
<?php
session_start();
include_once('../includes/connection.php');
if (isset($_SESSION['logged_in'])) {
if( strcasecmp($_SERVER['REQUEST_METHOD'],"POST") === 0) {
$_SESSION['postdata'] = $_FILES;
$error_array = array();
$upload_array = array();
$eupload_array = array();
$countfiles = count($_FILES['image']['name']);
$target_dir = "../images/";
for($i=0;$i<$countfiles;$i++) {
$filename = $_FILES['image']['name'][$i];
$target_file = $target_dir.$filename;
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$check = getimagesize($_FILES["image"]["tmp_name"][$i]);
echo $_FILES["image"]["tmp_name"][$i];
if($check !== false) {
$uploadOk = 1;
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
array_unshift($error_array, "Image $filename is not JPG, JPEG, PNG or GIF.");
$uploadOk = 0;
}
} else {
array_unshift($error_array, "File $filename is not an image.");
$uploadOk = 0;
}
if (file_exists($target_file)) {
array_unshift($error_array, "Image $filename already exists.");
$uploadOk = 0;
}
if ($_FILES["image"]["size"][$i] > 500000) {
$u = 1;
array_unshift($error_array, "Image $filename is too large.");
$uploadOk = 0;
}
if ($uploadOk == 0) {
array_unshift($eupload_array, "Sorry, image $filename was not uploaded.");
} else {
if (move_uploaded_file($_FILES['image']['tmp_name'][$i], $target_file)) {
array_unshift($upload_array, "Image $filename has been uploaded.");
} else {
array_unshift($eupload_array, "Sorry, $filename there was an error uploading your file.");
}
}
}
$_SESSION['error_array'] = $error_array;
$_SESSION['upload_array'] = $upload_array;
$_SESSION['eupload_array'] = $eupload_array;
header("Location:add_picture.php");
exit;
}
if( isset($_SESSION['postdata'])) {
$error_array = $_SESSION['error_array'];
$upload_array = $_SESSION['upload_array'];
$eupload_array = $_SESSION['eupload_array'];
unset($_SESSION['postdata']);
unset($_SESSION['error_array']);
unset($_SESSION['upload_array']);
unset($_SESSION['eupload_array']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel='stylesheet' href='../assets/style.css'/>
<title>CMS TEST</title>
</head>
<body>
<div class='container'>
<a href='add_picture.php' id='logo'><h2>Add Picture</h2></a>
<br/>
<small class='test_warning' style='color:#B83333'>
❶ Images should be JPG, JPEG, PNG or GIF format extentions. <br/>
❷ Images should not be above 500KB size.<br/>
❸ Please try to rename images before uploading for easier use.
</small> <br/><br/>
<form action='add_picture.php' method="post" enctype="multipart/form-data">
<input type="file" name="image[]" id="image" class="custom-file-input" multiple required/><br/><br/>
<input type="submit" value="Upload" name="submit_pic"/>
</form>
<?php if (!empty($upload_array)) { ?>
<small style='color:#00FF00'>
<?php echo '<br/><br/>', implode("<br/>",$upload_array), '<br/>'; ?>
</small>
<?php }?>
<?php if (!empty($eupload_array)) { ?>
<small style='color:#aa0000'>
<?php echo '<br/><br/>', implode("<br/>",$eupload_array), '<br/>'; ?>
</small>
<?php }?>
<?php if (!empty($error_array)) { ?>
<small style='color:#aa0000'>
<?php echo implode("<br/>",$error_array), '<br/>'; ?>
</small>
<?php }?>
<br/><br/>
<a href='index.php'>← Back</a>
<br/>
</div>
<a class='logout' href='logout.php'>Logout</a>
</body>
</html>
<?php
} else {
header('Location:index.php');
}
?>
Uploading the file is okay, but storing the path and the name of the file into my database is not.
This is my form html.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<form action="../model/uploadcredentials.php" method="POST" enctype="multipart/form-data">
<input type="file" name="fileToUpload" placeholder="Upload your credentials">
<button type="submit" name="submit">Upload</button>
</form>
And here is my action file
<?php
if(isset($_FILES["fileToUpload"]["name"]))
{
$target_dir = "../assets/credentials/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
$file = stripcslashes($_FILES["fileToUpload"]["name"]);
$file = mysqli_real_escape_string($con,$file);
$query = $con->query("INSERT INTO `portfolio`(p.desc) VALUES('$file')");
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
I`am trying to get the filename and the path to store it in my database
It's always worth checking for and reporting errors.
In your INSERT, (p.desc) is trying to find a table called p (usually an alias) which isn't defined. Try...
$query = $con->query("INSERT INTO portfolio(desc) VALUES('$file')");
(Without the p.).
BUT I still recommend using prepared statements, have a read of How to create a secure mysql prepared statement in php?
I am trying to upload an image via simple image upload script in PHP.
But, there is one problem which my testers have pointed out.
They are saying when they browse the image and then change the name of the source image and proceed with the upload.. Its not working.
Like before browe it was abc.jpg and after browe but before
uploading made it abc1.jpg
I tried to figure out this issue and found that when the above case is done I encounter a page not found error.
Can anyone tell me how can I fix this problem?
This is chrome specific only!
index.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
and upload.php
<?php
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
echo "Size=".$_FILES["fileToUpload"]["size"];
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
//echo $_FILES["fileToUpload"]["tmp_name"].'--'.$target_file;
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
I want to upload an file from form and when I click submit it uploads the file to the server and insert filename, filesize and filetype from that file. but I always get errors like:
Warning: "filesize(): "stat failed for "uploadedfile" in C:\wamp\www\Project2\members\uploaded.php on line 14"
Warning: filetype(): "Lstat failed for "uploadedfile" in C:\wamp\www\Project2\members\uploaded.php on line 15"
Here is my uploadfile.php:
<html>
<body>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<title>Uploading Page</title>
<b>Hello again,<?php echo $username ?>. From here, you can select your image or file and upload our database.</b>
<form action="uploaded.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form></tr>
</table>
</form>
</body>
</html>
and here is my uploaded.php:
<html>
<body>
<title>Uploading Page</title>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$filename = basename($_FILES["fileToUpload"]["name"]);
$filesize = filesize($_FILES["fileToUpload"]["name"]);
$filetype = filetype($_FILES["fileToUpload"]["name"]);
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "<br>File is an image - " . $check["mime"] . ".</br>";
$uploadOk = 1;
} else {
echo "<br>File is not an image.</br>";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "<br>Sorry, file already exists.</br>";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000000000000) {
echo "<br>Sorry, your file is too large.</br>";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "<br>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</br>";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "<br>Sorry, your file was not uploaded.</br>";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$sql=mysqli_query($con,"insert into datas(username,imagesnotes,size,type) values('$username','$filename','$filesize')");
if(!$sql)
{
echo "<br>there is a problem in MySQL please check again.</br>";
}
echo "<br>The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.</br>";
} else {
echo "<br>Sorry, there was an error uploading your file.</br>";
}
}
mysqli_close($con);
?>