I made an upload form called upload.php with action up.php so I upload images in the folder C:/xampp/htdocs/uploadimages and in the page upload.html I would like to make a dynamic slideshow containing all new and old pictures.
upload.php
<body>
<form action="up.php" method="post" enctype="multipart/form-data">
<h2>Upload File</h2>
<label for="fileSelect">Filename:</label>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
<p><strong>Note:</strong> Only <b> .jpg </b> and <b> .png </b> formats allowed to a max size of 3 MB.</p>
</form>
<div class="gallery">
<div class="gallery-image">
<?php
$directory = "C:/xampp/htdocs/uploadimages";
$images = glob($directory . "/*.jpg");
foreach($images as $image)
{
echo " <img src=\"basename($image)\"> ";
echo "<br>" ;
}
?>
</div>
</body>
up.php
<?php
$target_dir = "C:/xampp/htdocs/uploadimages/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if (file_exists($target_file)) {
echo "File already exists. <br>";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 3 * 1024 * 1024) {
echo "Your file is too large. <br>";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png") {
echo "Only JPG and PNG files are allowed. <br>";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded. <br>";
} else {
echo "There was an error uploading your file. <br>";
}
}
?>
I tried the basename($image) inside a for-each loop but nothing worked
Related
I want to Upload imagename into database and directory then want to show it on preview page (preview.php ). but its not working at all. as i am new to programming, so i need support. here the problem is, neither the image save into directory nor into the database.
here is the form page (form5.php) where we select image and after click on submit it goes to next page pptupload.php.
<form method="post" action="pptupload.php?appid=<?php echo $appid; ?>" enctype='multipart/form-data'>
<table>
<tbody>
<tr>
<img id="pptimg" src="#" width="600px" height="300px" alt="Scanned Passport" />
<input type="file" id="file" name="file" />
</tr>
<tr>
<input name='but_upload' type="submit" class="btn btn-primary" value="Save and Continue">
</tr>
</tbody>
</table>
</form>
here is the next Page (pptupload.php ) . On this Page, I want redirect to next page preview.php after successful upload of image and if it failed then show error and return back to the image selection page called form5.php .
<?php
session_start();
$appid = $_GET['appid'];
include("connect.php");
if(isset($_POST['upload'])){
$name = $_FILES['file']['name'];
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
// Select file type
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Valid file extensions
$extensions_arr = array("jpg","jpeg","png","gif");
// Check extension
if( in_array($imageFileType,$extensions_arr) ){
// Upload file
move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name);
}
}
// Insert record
$query = "insert into payments(app_id, pptimg) values('$appid','$name')";
if (mysqli_query($connect,$query)){
echo "Image were updated successfully.";
header("Location: preview.php?appid=".$appid);
}else{
echo "Photo not uploaded".mysqli_error($connect);
}
?>
Check below code :
<?php
$target_dir = "uploads/";
$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;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$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 ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
I want to prepare 5 photo upload form and user need to select all 5 photo at once and the submit the form
then i need to store all 5 photos in the Target Directory by renaming image as "1.jpg, 2.jpg, 3.jpg, 4.jpg, 5.jpg". I stored only one image but after add For loop for save more than one file its not working ....please support.
// Image Upload FORM
<form action="saveinntion.php" method="post" enctype="multipart/form-data">
<h1>Upload Your Innovation</h1
<fieldset>
<legend><span class="number">4</span>Upload Images</legend>
<input type="file" name="img1" id="img1" >
</br>
<input type="file" name="img2" id="img2">
</br>
<input type="file" name="img3" id="img3">
</br>
<input type="file" name="img4" id="img4">
</br>
<input type="file" name="img5" id="img5">
</br>
</fieldset>
<button type="submit">Submit</button>
</form>
saveinntion.php file
<?php
include("dbconnection.php");
$target_dir = "Upload/";
$img=$_POST['img'];
for ($i = 0; $i < 5; $i++) {
$target_file = $target_dir . basename($_FILES['$img[]']["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES['$img[]']["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "jpeg" ) {
echo "Sorry, only JPG & JPEG files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES['$img[]']["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES['$img[]']["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
header("Location: upload1.php?id=$msg");
?>
Your problem is with your naming. as #Twinfriends said in the command it is highly unlikely to upload any file
replace $_FILES['$img[]'] with $_FILES['img'.($i+1)] in your code.
line # 5
$target_file = $target_dir . basename($_FILES['img'.($i+1)]["name"]);
line #13
if ($_FILES['img'.($i+1)]["size"] > 500000) {
line #25
if (move_uploaded_file($_FILES['img'.($i+1)]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES['img'.($i+1)]["name"]). " has been uploaded.";
To renaming file.
$imageFileType = pathinfo($_FILES['img'.($i+1)]["name"],PATHINFO_EXTENSION);
$target_file = $target_dir . ($i+1).".".$imageFileType;
I now have a code to upload a single file to my website, I want to change this to a multiple file upload. I want people to be able to select multiple files at once. Does someone know what I need to change about my code?
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
<?php
$target_dir = "gallery-images/";
$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
if ($_FILES["fileToUpload"]["size"] > 500000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $imageFileType != "JPG" ) {
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.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
All you need to make work it on front end is simply add MULTIPLE attribute to your input - it also works with drag and drop with couple browsers.
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload" multiple>
<input type="submit" value="Upload Image" name="submit">
</form>
Let me know if You want to send it one by one in ajax
Whenever I am uploading any file it says,
File is an image - image/png.Sorry, there was an error uploading your file.
I am using post method and enctype="multipart/form-data". My image file is also proper.
<?php
if(isset($_POST['submit'])){
$target_dir = "pics/";
$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
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 ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="usr">Select image to upload:</label>
<input type="file" name="fileToUpload" id="fileToUpload">
</div>
<div class="form-group">
<label for="usr">URL:</label>
<input type="text" class="form-control" id="usr" name="url">
</div>
</div>
</div>
<button type="submit" name="submit" class="btn btn-success">Add</button>
<button type="reset" class="btn btn-warning">Reset</button>
</form>
Your file cannot be moved.
Check the path to move to
Check the permissions of the directory
Check your access to the tmp directory
You use a relative path: $target_dir = "pics/";
Change your path to:
$target_dir = $_SERVER['DOCUMENT_ROOT'] . '/pics/';
I have just setup a fileupload.php so that I can upload files but am interesting in having the files instantly be setup on the page that I want it with the css that I assign. Is there a way to do this? I would like to have three columns of photos that viewers can see as I upload the files so I do not have to manually add it into the code every single time I want to add a photo to the page.
I already know how to make a grid with the photos but am really interested in how to actually have all the photos uploaded to the page to instantly be placed on that grid.
My graphics.php page :
<?php include 'includes/header.php' ?>
<div class="graphics_content">
<div class="page_top_image">
<img src="includes/img/heartfx_graphics.png" class="img-responsive" alt="Responsive image">
</div>
</div>
<div class="photo_upload">
<div class="photo_upload_form">
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<br>
<input type="file" name="fileToUpload" id="fileToUpload">
<br>
<input type="submit" value="Upload Image" name="submit">
</form>
</div>
</div>
My upload.php page :
<?php
$target_dir = "uploads/";
$target_file = $target_dir .basename($_FILES["fileToUpload"]["name"]);
$uploadOK = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
//Check if image file is an actual image or a 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
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOK = 0;
}
// Allow certain file formats
if($imageFileType != "png") {
echo "Sorry, only PNG 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.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
Thank you in advance for any help. If anything is needed from my code that you would like me to provide I can do so.
Best regards,
Codi