if (isset($_POST['submit2'])){
$file_name = $_FILES['file']['name'];
$file_type = $_FILES['file']['type'];
$file_size = $_FILES['file']['size'];
$file_tem_loc = $_FILES['file']['tmp_name'];
$file_store = "function/upload/".$file_name;
$AAnimalName = mysqli_real_escape_string($conn, $_POST['Aanimal']);
$Abreed = mysqli_real_escape_string($conn, $_POST['Abreed']);
$Asex = mysqli_real_escape_string($conn, $_POST['Asex']);
$Acolor = mysqli_real_escape_string($conn, $_POST['Acolor']);
$Amark = mysqli_real_escape_string($conn, $_POST['Amark']);
$file_name = mysqli_real_escape_string($conn, $_FILES['file']['name']);
$sql = "INSERT INTO adoption (AAnimalName, Abreed, Asex, Acolor, Amark, image)
VALUES ('$AAnimalName', '$Abreed', '$Asex', '$Acolor', '$Amark', '$file_name')";
$result = mysqli_query($conn,$sql);
if(move_uploaded_file($file_tem_loc, $file_store)){
echo "";
$folder = "function/upload/";
if (is_dir($folder)){
if ($handle = opendir($folder))
{
while (($file = readdir($handle)) !=false)
{
if ($file == '.' || $file = '..') continue;
echo '<img src = "function/upload/'.$file.'" width = "250" height="250">';
}
closedir($handle);
}
}
}
As seen in my code. The image upload is successful and stores exactly at "function/upload/" code on my display doesn't work. What i understand from the code that i have on the display part, I'm just trying to open a dir. Help anyone?
Uploaded images display part of code is not working because of this line
Your code
if ($file == '.' || $file = '..') continue;
You miss one = symbol.
Fixed code
if ($file == '.' || $file == '..') continue;
Your code always uses a function "continue" and skips all results.
That is the reason you don't see any images.
Related
I would like to upload some files to my server. So far, they have been successfully stored in a MySQL database, but the problem is that the uploaded file is not found in a folder that is located on the server.
Here's my code:
$dir = $filename;
$target_dir = "file/$dir/";
if( is_dir($target_dir) === false )
{
mkdir($target_dir);
}
if(isset($_POST["submit"])) {
$formatfile = array('pdf');
$filetest= $_FILES['filetest']['name'];
$x = explode('.', $filetest);
$existence= strtolower(end($x));
$pdfsize = $_FILES['filetest']['size'];
$file_tmp = $_FILES['filetest']['tmp_name'];
if(in_array($existence, $formatfile) === true){
if($pdfsize < 1044070){
move_uploaded_file($file_tmp, "$target_dir.$filetest");
}
}
}
$sql = "INSERT INTO test (filename) VALUES ('$filetest')";
Is there anything I missed?
Please change $formatfile = array('pdf') to $formatfile = array('.pdf'=>'application/pdf');
$dir = $filename; Whis is vlaue of $filename ?
please use like this.
$dir = '/var/www/uploads/';
if(in_array($existence, $formatfile)){
if($pdfsize < 1044070){
move_uploaded_file($file_tmp, "$target_dir.$filetest");
}
}
}
whenever you are uploading a file.try the full path.
<?php
$dir = $filename;
$target_dir = dirname(__FILE__).'/file/'.$dir.'/';
if (is_dir($target_dir) === false) {
mkdir($target_dir);
}
if (isset($_POST['submit'])) {
$formatfile = array('pdf');
$filetest = $_FILES['filetest']['name'];
$x = explode('.', $filetest);
$existence = strtolower(end($x));
$pdfsize = $_FILES['filetest']['size'];
$file_tmp = $_FILES['filetest']['tmp_name'];
if (in_array($existence, $formatfile) === true) {
if ($pdfsize < 1044070) {
move_uploaded_file($file_tmp, $target_dir.$filetest);
}
}
}
$sql = 'INSERT INTO test (filename) VALUES ('.$filetest.')';
$dir = $filename;
$target_dir = "file/$dir/";
if( is_dir($target_dir) === false )
{
mkdir($target_dir);
}
if(isset($_POST["submit"])) {
$formatfile = array('pdf');
$filetest= $_FILES['filetest']['name'];
$x = explode('.', $filetest);
$existence= strtolower(end($x));
$pdfsize = $_FILES['filetest']['size'];
$file_tmp = $_FILES['filetest']['tmp_name'];
if(in_array($existence, $formatfile) === true){
if($pdfsize < 1044070){
//Remove ""
move_uploaded_file($file_tmp, $target_dir.$filetest);
//If file upload then and then execute query.
$sql = "INSERT INTO test (filename) VALUES ('$filetest')";
}else{
//Fire error
}
}else{
//Fire error
}
}else{
//Fire error
}
i want to upload multiple images using this code but i can`t , i think it would be by using for each but every time i try the errors come everywhere
i want to insert the whole images paths in one column and after each picture ','
here is the php code
if (isset($_POST['upload_file'])) {
$date = date('Y-m-d h:i:s');
if ($_FILES['image']['name'] != '') {
$temp = explode('.', $_FILES['image']['name']);
$image_name = round(microtime(true)) . '.' . end($temp);
$image_tmp = $_FILES['image']['tmp_name'];
$image_size = $_FILES['image']['size'];
$image_ext = pathinfo($image_name,PATHINFO_EXTENSION);
$image_path = '../media/file/'.$image_name;
$image_db_path = '../media/file/'.$image_name;
if ($image_size < '50000000') {
if ($image_ext == 'jpg' || $image_ext == 'png' || $image_ext =='gif' || $image_ext == 'jpeg') {
if (move_uploaded_file($image_tmp, $image_path)) {
$ins_sql = "INSERT INTO files (imgs_paths, date) VALUES ('$image_db_path', '$date')";
if (mysqli_query($conn,$ins_sql)) {
header('Location: index.php');
}else{
$error = '';
}
}else{
$error = '';
}
}else{
$error = '';
}
}else{
$error = '';
}
} else{
$error = '';
}
}
and here the html code
<input type="file" accept="image/*" name="image[]" multiple/>
You're right about using foreach. If you need to upload multiple, you'll need to use it. And it's as simple as:
foreach($_FILES['image'] as $i => $file){
///... other code
$temp = explode('.', $_FILES['image'][$i]['name']);
$image_name = round(microtime(true)) . '.' . end($temp);
$image_tmp = $_FILES['image'][$i]['tmp_name'];
$image_size = $_FILES['image'][$i]['size'];
///... rest of code
}
Since it's an array of images, you need to access each element. Notice how we're using the array index($i) to access the current element in the loop.
As #RiggsFolly stated, you can also use $file in your loop as it is also the current element in the loop. (Instead of $_FILES['image'][$i]...)
I have this script:
$uploadsDirectory = dirname($_SERVER['SCRIPT_FILENAME']) .'/slides/head/';
if ($handle = opendir($uploadsDirectory)) {
$uplo = array();
while (false !== ($file = readdir($handle))) {
array_push($uplo, $file);}
sort($uplo,SORT_NATURAL | SORT_FLAG_CASE);
$user = array();
foreach($uplo as $fname) {
if($fname != ".." && $fname != "."){
if(substr($fname,0,1) != "_")
echo "<div class='bgitem' id='head'>$fname</div>";
else
array_push($user, "$fname");}}
closedir($handle);}
It works fine, but how can I make it so it only shows the pictures? (I have other files that aren't photos, so it displays a broken picture instead.)
A simple way would be to have it test whether the file is an image in the same line where you test if the file is a parent directory or the current directory (if($fname != ".." && $fname != "."){)
You can use getimagesize() to determine if the file is any kind of image. If it is not an image, it will return zero.
$uploadsDirectory = dirname($_SERVER['SCRIPT_FILENAME']) .'/slides/head/';
if ($handle = opendir($uploadsDirectory)) {
$uplo = array();
while (false !== ($file = readdir($handle))) {
array_push($uplo, $file);}
sort($uplo,SORT_NATURAL | SORT_FLAG_CASE);
$user = array();
foreach($uplo as $fname) {
if($fname != ".." && $fname != "." && getimagesize($fname) != 0){ //Tests if file is an iamge
if(substr($fname,0,1) != "_")
echo "<div class='bgitem' id='head'>$fname</div>";
else
array_push($user, "$fname");}}
closedir($handle);}
Solution for you:
$extension = explode(".", $fname);
$extension = (isset($extension) && count($extension) > 0)?strtolower($extension[count($extension) -1]):null;
if(in_array($extension, ['jpg', 'jpeg', 'png', 'gif'])){
//Show the image
}else{
//dont show image
}
I'm using the following to create a list of my files in the 'html/' and link path.
When I view the array it shows, for example, my_file_name.php
How do I make it so the array only shows the filename and not the extension?
$path = array("./html/","./link/");
$path2= array("http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/html/","http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/link/");
$start="";
$Fnm = "./html.php";
$inF = fopen($Fnm,"w");
fwrite($inF,$start."\n");
$folder = opendir($path[0]);
while( $file = readdir($folder) ) {
if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
$folder2 = opendir($path[1]);
$imagename ='';
while( $file2 = readdir($folder2) ) {
if (substr($file2,0,strpos($file2,'.')) == substr($file,0,strpos($file,'.'))){
$imagename = $file2;
}
}
closedir($folder2);
$result="<li class=\"ui-state-default ui-corner-top ui-tabs-selected ui-state-active\">\n\n$file2\n<span class=\"glow\"><br></span>
</li>\n";
fwrite($inF,$result);
}
}
fwrite($inF,"");
closedir($folder);
fclose($inF);
pathinfo() is good, but I think in this case you can get away with strrpos(). I'm not sure what you're trying to do with $imagename, but I'll leave that to you. Here is what you can do with your code to compare just the base filenames:
// ...
$folder = opendir($path[0]);
while( $file = readdir($folder) ) {
if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
$folder2 = opendir($path[1]);
$imagename ='';
$fileBaseName = substr($file,0,strrpos($file,'.'));
while( $file2 = readdir($folder2) ) {
$file2BaseName = substr($file2,0,strrpos($file2,'.'));
if ($file2BaseName == $fileBaseName){
$imagename = $file2;
}
}
closedir($folder2);
$result="<li class=\"ui-state-default ui-corner-top ui-tabs-selected ui-state-active\">\n\n$file2\n<span class=\"glow\"><br></span>
</li>\n";
fwrite($inF,$result);
}
}
I hope that helps!
I'm loading a folder full of images in, to create a jQuery Image Gallery.
There are currently 100 images being loaded in to create the gallery. I've got all that to load without issue.
All I want to do is make the image(s) that are loaded, load in randomly.
How do I achieve this?
My code is :
<?php
$folder = "images/";
$handle = opendir($folder);
while(($file = readdir($handle)) !== false) {
if($file != "." && $file != "..")
{
echo ("<img src=\"".$folder.$file."\">");
}
}
?>
Thanks in advance.
Just store all the image paths in an array and do a random shuffle of the array. And then echo the elements
<?php
$folder = "images/";
$handle = opendir($folder);
$imageArr = array();
while(($file = readdir($handle)) !== false) {
if($file != "." && $file != "..")
{
$imageArr[] = $file;
}
shuffle($imageArr); // this will randomly shuffle the image paths
foreach($imageArr as $img) // now echo the image tags
{
echo ("<img src=\"".$folder.$img."\">");
}
}
?>
Traverse the directory and store the image file names into an array and randomly select path names from the array.
A basic example:
$dir = new DirectoryIterator($path_to_images);
$files = array();
foreach($dir as $file) {
if (!$fileinfo->isDot()) {
$files[] = $file->getPathname();
}
}//$files now stores the paths to the images.
You can try something like this:
<?php
$folder = "images/";
$handle = opendir($folder);
$picturesPathArray;
while(($file = readdir($handle)) !== false) {
if($file != "." && $file != "..")
$picturesPathArray[] = $folder.$file;
}
shuffle($picturesPathArray);
foreach($picturesPathArray as $path) {
echo ("<img src=\"".$path."\">");
}
?>