I've been working on a php script which creates a folder and saves posted images in the created folder, the folders get created but the images I aren't saved.
Below is the script:
if (isset($_FILES['images'])) {
mkdir("files/test/".$new."/", 0755, true);
foreach ($_FILES['images']['tmp_name'] as $key => $value) {
if (!(empty($_FILES['images']['tmp_name'][$key]) || $_FILES['images']['tmp_name'][$key] == 'none')) {
$i_slika = 1;
$extenzion = strtolower(end(explode(".", $_FILES['images']['name'][$key])));
$file = "files/nekretnina/".$new."/".$i_slika.".".$extenzion;
while (file_exists($file)) {
$i_slika++;
$file = "files/nekretnina/".$new."/".$i_slika.".".$extenzion;
}
if (!#move_uploaded_file($_FILES['images']['tmp_name'][$key], $file)) {
$err_text = $err_text. 'Error: '.$_FILES['images']['name'][$key].'<br />';
} else {
include_once "thumbz.class.php";
$pic = new thumbz($file);
$pic->dimensionImage("resize",640);
$pic->addParam('l',0);
$pic->render($file);
}
}
}
}
You need to use $_FILES['images']['tmp_name'][$key] instead of $_FILES['slika']['tmp_name'][$key].
You have the wrong index name!
Try Using:
if (isset($_FILES['images'])) {
mkdir("files/test/".$new."/", 0755, true);
foreach ($_FILES['images']['tmp_name'] as $key => $value) {
if (!(empty($_FILES['images']['tmp_name'][$key]) || $_FILES['images']['tmp_name'][$key] == 'none')) {
$i_slika = 1;
$extenzion = strtolower(end(explode(".", $_FILES['images']['name'][$key])));
$file = "files/test/".$new."/".$i_slika.".".$extenzion;
while (file_exists($file)) {
$i_slika++;
$file = "files/test/".$new."/".$i_slika.".".$extenzion;
}
if (!#move_uploaded_file($_FILES['images']['tmp_name'][$key], $file)) {
$err_text = $err_text. 'Error: '.$_FILES['images']['name'][$key].'<br />';
} else {
include_once "thumbz.class.php";
$pic = new thumbz($file);
$pic->dimensionImage("resize",640);
$pic->addParam('l',0);
$pic->render($file);
}
}
}
}
Related
I use laravel 4.2
I have more than 1,000 images in the project; these images have been occupied by host users after multiple uploads. Please edit the sample instance in such a way that it will erase all images except images that have both the name and the file stored.
public function del_image()
{
$scan = scandir('uploads/evidence');
foreach($scan as $file)
{
if (!is_dir($file))
{
$list = DB::table('evidence')
->where('profile_img',$file)
->select('profile_img')->get();
echo '<pre>';print_r($list); echo '</pre>';
}
}
//echo $list."<br>";
//echo '<pre>';print_r($list); echo '</pre>';
}
Thanks.
$scan = scandir('uploads/evidence');
$files = DB::table('evidence')->pluck('profile_img');
$protectTheseImages = [];
foreach($files as $file) {
$protectTheseImages[] = $file;
}
$diff = array_diff($scan, $protectTheseImages);
foreach($diff as $file) {
if (!is_dir($file)) {
unlink($file);
}
}
I think You should use this code.
$scan = scandir('uploads/evidence');
$files = DB::table('evidence')->pluck('profile_img');
$protectTheseImages = [];
foreach((array)$files as $file) {
$protectTheseImages[] = $file;
}
//dd($protectTheseImages);
$diff = array_diff($scan, $protectTheseImages);
foreach($diff as $file) {
if (!is_dir($file)) {
echo $file."<br>";
$image_path = '/folder_path/uploads/evidence/'.$file;
unlink($image_path);
}
}
Hope This Code Helps you.
this code i updated to :
$scan = scandir('uploads/evidence');
$files = DB::table('evidence')->pluck('profile_img');
$protectTheseImages = [];
foreach((array)$files as $file) {
$protectTheseImages[] = $file;
}
//dd($protectTheseImages);
$diff = array_diff($scan, $protectTheseImages);
foreach($diff as $file) {
if (!is_dir($file)) {
echo $file."<br>";
//unlink($file);
}
}
but, line echo $file."<br>"; just show all images in directory !!!
I am working on a Laravel project, the code saves the image inside a folder created with the title coming from a post. The problem is that while adding post it does not gives a warning saying filename too long but while deleting the post it says filename too long in demo.php/uploads/filename.php.
Is it really because of long folder name or something else?
The following code is called for deleting the folder while deleting a post.
function rmdir_recursive($dir) {
if (is_array($dir) || is_object($dir))
{
foreach(scandir($dir) as $file) {
if ('.' === $file || '..' === $file) continue;
if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
else unlink("$dir/$file");
}
}
if (is_string($dir)) {
rmdir($dir);
}
}
Following the function to add post:
function create()
{
if($_SERVER['REQUEST_METHOD']=="POST")
{
$pid = rand(1000,9000);
$title = $_POST['title'];
$descpt = $_POST['description'];
$push = isset($_POST['send_push']) ? $_POST['send_push'] : "";
$feature_image = array();
$fy = $_POST['fy'];
if(empty($title) || empty($descpt) || empty($fy))
{
array_push($this->errors, MEND_FIELD_ERROR);
return;
}
if(!empty($_FILES['feature_image']['name'][0]))
{
$image = $_FILES['feature_image'];
$allowed_ext = array('jpeg','jpg','png','pdf','docx');
$allowed_size = 20000000;
foreach($image['name'] as $pos=>$image_name)
{
$dir = "./cdn/uploads/notice/".$title;
$tmp = $image['tmp_name'][$pos];
$img_size = $image['size'][$pos];
$img_error = $image['error'][$pos];
$img_ext = explode('.', $image_name);
$img_name = $img_ext[0];
$img_ext = strtolower(end($img_ext));
if(in_array($img_ext, $allowed_ext))
{
if($img_size <= $allowed_size)
{
if(!file_exists($dir))
{
mkdir($dir);
}
$image_new_name = $img_name.'$$'.uniqid('', true).'.'.$img_ext;
$upload_destination = $dir.'/'.$image_new_name;
if(move_uploaded_file($tmp, $upload_destination))
{
array_push($feature_image, $image_new_name);
}
else
{
array_push($this->errors, $img_error);
return;
}
}
}
else
{
array_push($this->errors, $img_ext.' is not an allowed file extension.');
return;
}
}
}
$s_feature_image = json_encode($feature_image, JSON_UNESCAPED_UNICODE);
$statement = $this->db->prepare("INSERT INTO `notice` (`pid`,`title`,`descpt`,`date`,`photo`,`fy`)
VALUES (?,?,?,?,?,?)");
if($statement->execute([$pid,$title,$descpt,DAT, $s_feature_image, $fy]))
{
if($push == "checked")
{
$descpt = strip_tags($descpt);
$tek = array("message"=>$descpt,"title"=>$title);
$tokens = $this->getTokens();
$this->push_notification($tokens,$tek);
}
ExitThis::send_to(URL.'notice?id='.$pid);
}
else
{
array_push($this->errors, DATABASE_ERROR);
return;
}
}
}
Note: The title will be Nepali character.
I don't know this was right but deleting folder if that is not string help me. I did following changes in my code:
function rmdir_recursive($dir) {
if (is_array($dir) || is_object($dir))
{
foreach(scandir($dir) as $file) {
if ('.' === $file || '..' === $file) continue;
if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
else unlink("$dir/$file");
}
}
if(!is_string($dir)){
rmdir($dir);
}
}
I have a php script which upload a photo from an Ajax call, and i want to upload the photo two times, one with standard size and another one compressed. Does anyone know how to do it with this following code? I tried to compress the image with scripts that I found on stackoverflow, but I can't do it correctly. Everytime appears an black photo.
I got this code:
$data = array();
if (isset($_GET['files'])) {
$error = false;
$files = array();
$uniqid = uniqid();
$uploaddir = '../../images/'.$uniqid;
foreach($_FILES as $file) {
if (move_uploaded_file($file['tmp_name'], $uploaddir.basename($file['name']))) {
$files[] = $uploaddir.$file['name'];
} else {
$error = true;
}
}
$data = ($error) ? array('error' = > 'There was an error uploading your files') : array('files' = > $files);
} else {
$arr - > image = $file['name'];
$_SESSION['image'] = "img-".$file['name'];
$arr - > ok = "ok";
$data = array('success' = > 'Form was submitted', 'formData' = > $file['name']);
}
Thank you all!
Try using basename() also in your $files[] array insertion:
Change this:
foreach($_FILES as $file) {
if (move_uploaded_file($file['tmp_name'], $uploaddir.basename($file['name']))) {
$files[] = $uploaddir.$file['name'];
} else {
$error = true;
}
}
To this:
foreach($_FILES as $file) {
if (move_uploaded_file($file['tmp_name'], $uploaddir.basename($file['name']))) {
$files[] = $uploaddir.basename($file['name']);
} else {
$error = true;
}
}
I want to delete all files in a folder except files containing:
Specific file names
Specific file extensions
The following code succeeds in the above first point, but not the second point.
function deletefiles()
{
$path = 'files/';
$filesToKeep = array(
$path."example.jpg",
$path."123.png",
$path."*.mkv"
);
$dirList = glob($path.'*');
foreach ($dirList as $file) {
if (! in_array($file, $filesToKeep)) {
if (is_dir($file)) {
rmdir($file);
} else {
unlink($file);
}//END IF
}//END IF
}//END FOREACH LOOP
}
How can I achieve both conditions?
You need to change a bit your function:
<?php
function deletefiles()
{
$path = 'files/';
$filesToKeep = array(
$path . "example.jpg",
$path . "123.png",
);
$extensionsToKeep = array(
"mkv"
);
$dirList = glob($path . '*');
foreach ($dirList as $file) {
if (!in_array($file, $filesToKeep)) {
if (is_dir($file)) {
rmdir($file);
} else {
$fileExtArr = explode('.', $file);
$fileExt = $fileExtArr[count($fileExtArr)-1];
if(!in_array($fileExt, $extensionsToKeep)){
unlink($file);
}
}//END IF
}//END IF
}
}
Hi
i am trying to build simple directory browser to browse folders and sub-folders uing php RecursiveDirectoryIterator .. i need help of how to create this. i have started with the following code.
$dir = dirname(__FILE__); //path of the directory to read
$iterator = new RecursiveDirectoryIterator($dir);
foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
if (!$file->isFile()) { //create hyperlink if this is a folder
echo "" . $file->getFilename() . "\";
}else{ //do not link if this is a file
$file->getFilename()
}
}
Allow me to code that for you....
<?php
$root = __DIR__;
function is_in_dir($file, $directory, $recursive = true, $limit = 1000) {
$directory = realpath($directory);
$parent = realpath($file);
$i = 0;
while ($parent) {
if ($directory == $parent) return true;
if ($parent == dirname($parent) || !$recursive) break;
$parent = dirname($parent);
}
return false;
}
$path = null;
if (isset($_GET['file'])) {
$path = $_GET['file'];
if (!is_in_dir($_GET['file'], $root)) {
$path = null;
} else {
$path = '/'.$path;
}
}
if (is_file($root.$path)) {
readfile($root.$path);
return;
}
if ($path) echo '..<br />';
foreach (glob($root.$path.'/*') as $file) {
$file = realpath($file);
$link = substr($file, strlen($root) + 1);
echo ''.basename($file).'<br />';
}