Apache HTTP error 500 on file upload [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Whenever I try to upload files to my website I get 500 error and notification that server couldn't handle request. I tried to configure upload_max_filesize in both, php.ini and .htaccess, but nothing works. I also tried to set value of MaxRequestLen in apache2.conf:
<IfModule mod_fcgid.c>
MaxRequestLen 20000000
</IfModule>
EDIT: I can't post whole code because it contains sensitive data, here are parts of it:
$files = $_FILES["images"]["name"];
$tmpNames = $_FILES["images"]["tmp_name"];
$archiveName = time();
$folder = "./uploads/";
$price = 0;
foreach ($imagesFormats as &$imageFormat) {
if (!file_exists($folder.$archiveName."/".$imageFormat)) {
if (!mkdir($folder.$archiveName."/".$imageFormat, 0777, true)) {
addError("Error while trying to create directory.");
}
}
}
foreach ($_FILES["images"]["error"] as $key => $err) {
if ($err == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["images"]["tmp_name"][$key];
$name = basename($_FILES["images"]["name"][$key]);
move_uploaded_file($tmp_name, $folder.$archiveName."/".$imagesFormats[$key]."/".$name);
}
}
if (Zip($folder.$archiveName."/", $folder.$archiveName.".zip")) {
rrmdir($folder.$archiveName."/");
} else {
addError("Error on archiving.");
}

Problem is solved - I didn't knew that mb_strlen() function is not available by default in PHP but should be installed.

Related

how to rename uploaded multiple images so that is not replacing the existing files [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a site where users can upload multiple images i am having problems as the new uploaded files are replacing the existing files in the images directory that has the same name. please help i am new to php
if (isset($_FILES['images']))
{
foreach ($_FILES['images']['tmp_name'] as $key => $tmp_name)
{
$target = "../uploads/";
$target = $target.$_FILES['images']['name'][$key];
if(move_uploaded_file($tmp_name, $target))
{
$fname=$_FILES['images']['name'][$key];
$target = "PostAd/uploads/". $fname;
mysql_query("INSERT INTO `upload_data`(`clid`, `id`, `Imgpath`,`target`) VALUES ('$clid','$id','$fname','$target')");
$target="";
}
}
if ($sql){
header( "Location: /Myconnec/PostAd/CampusLife/confirm.php?clid=$clid");
ob_end_flush();
} else {
$error_msg = 'ERROR: Problems arose during the information exchange, please try again later.';
}
you can add a timestamp like this...
if (isset($_FILES['images'])) {
foreach ($_FILES['images']['tmp_name'] as $key => $tmp_name)
{
$target = $target.$_FILES['images']['name'][$key];
$ext = pathinfo($target, PATHINFO_EXTENSION);
$rand = rand();
$strip_ext = substr($target, 0, strlen($target) - strlen($ext));
$timestamp = str_replace(" ","",microtime());
$target = "$strip_ext.$timestamp.$rand.$ext";
if(move_uploaded_file($tmp_name, "../upload/$target"))
{
$fname=$_FILES['images']['name'][$key];
$target = "PostAd/uploads/".$target;
mysql_query("INSERT INTO `upload_data`(`clid`, `id`, `Imgpath`,`target`) VALUES ('$clid','$id','$fname','$target')");
$target="";
}
}
if ($sql){
header( "Location: /Myconnec/PostAd/CampusLife/confirm.php?clid=$clid");
ob_end_flush();
} else {
$error_msg = 'ERROR: Problems arose during the information exchange, please try again later.';
}

Build an array from .INI file using PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I've my .ini file as follows
languages.ini
--------------------
fr = "French"
en = "English"
Now, I'd like to read the file and keep the keys and values in the form of an array using PHP. I'm very new to PHP. I hope any one help me in achieving this. This is my following code.
public function GetLanguages()
{
$langdir = ISC_BASE_PATH.'/language';
$skip = Array (
'.',
'..',
'CVS',
'.svn',
);
$langs1 = array();
$dh = opendir($langdir);
while (($file = readdir($dh)) !== false) {
if (!is_file($langdir.'/'.$file.'/languages.ini')) {
continue;
}
$langs1[] = $file;
}
echo "FileLL:".$file;
foreach ($langs1 as $key) {
echo "Store languagesss::".$key."<br>";
}
return $langs1;
}
Thank you in advance.
You are looking for PHP's ini parse function:
parse_ini_file
Just call $arrayResult = parse_ini_file($path_to_file)

Oldest file in a directory - including all its sub directories using php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I need to find the oldest file in a directory, oldest file in all (including, directories, subdirectories of a particular folder)
Folder 1
- dir 1
- dir 1.1
- file 1
- dir 2
- dir 2.1
- file 2
if file 2 is oldest , I need to able to get oldest file (file 2) by passing main directory name (Folder 1)
my solutions is
function get_oldest_file($dir) {
$filemdate = array();
print $dir.PHP_EOL;
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
$files[] = $file;
print $file.PHP_EOL;
}
foreach ($files as $eachfile) {
if (is_file($dir.eachfile)) {
$file_date[$eachfile] = filemtime($dir.$eachfile);
print $filemdate[$eachfile].PHP_EOL;
}
}
}
closedir($handle);
asort($filemdate, SORT_NUMERIC);
reset($filmdate);
$oldest = key($filemdate);
print "Oldest is : ".$oldest;
return $oldest;
}
echo get_oldest_file("/path/---")
Thanks !
What about?
function workerFunction($currentDir, $oldestFile)
foreach (glob($currentDir.'/*') as $file) {
if (is_dir($file)) {
if ((basename($file)!='.') && (basename($file)!='..')) {
$oldestFile = workerFunction($file, $oldestFile);
}
} else {
$mtime = filemtime($file);
if ($mtime <= $oldestFile['mtime']) {
$oldestFile['mtime'] = $mtime;
$oldestFile['path'] = $file;
}
}
}
return $oldestFile;
}
function searchForOldestFile($dir) {
$oldestFile['mtime'] = time();
$oldestFile['path'] = null;
$oldestFile = workerFunction($dir, $oldestFile);
return $olderstFile['path'];
}
I don't have the PHP environment for debugging, but at least with some little fixes it could work as you need.
Let me explain, so you would be able to use it easily:
searchForOldestFile function is the interface that your script should call; workerFunction does the "magic" (if it work :)), keeping in $oldestFile a reference to the current oldestFile.

PHP function to remove a directory and all its sub content. [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
}
}
reset($objects);
rmdir($dir);
}
}
I have this function to delete a directory and all its contents (sub directories and sub files).
This function works great and can delete over 5k files in just a second or two.
But does anyone have any suggestions on optimizing this function?
Also ... if anyone has any "system" or method to securely host custom php functions on one server and call them on other servers let me know... that would be awesome as I have a huge collection of functions and I work off of 3 servers and would love to have them all in one location. I use cPanel's global prepend to include all my functions in all my php files easily and that works extremely well BUT if there was a way to simply call a remotely hosted PHP file into the prepend file that is included in each file on the server that would be superb... Any suggestions for a similar setup would be awesome.
while loop should be faster then foreach, also order of if statement does matter.
function removeDirectory($path)
{
$path = rtrim($path, '/').'/';
$files = scandir($path);
$i = count($files);
while (--$i) {
$file = $files[$i];
$fullpath = $path.$file;
is_file($fullpath)
? #unlink($fullpath)
: ($file != '.' and $file != '..' and removeDirectory($fullpath));
}
rmdir($path);
return true;
}
if you are allowed to to do shell execution you can execute the simple linux function
$out = shell_exec('rm -rf /path/to/directory');
and you can do
var_dump($out);
to check out the result if it was removed or not.

How to read and write images and video with PHP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am creating a downloadable zip file. In that zip, I want to add php, html, and images and videos.
All of them are in other folders.
I can read and write PHP and HTML files from other files/folders.
But I don't know how to read and write (or move from one folder to anther folder) images and videos.
Please try this code
function move_files($dir)
{
if(is_dir($dir))
{
if($handle = opendir($dir))
{
while(($file = readdir($handle)) !== false)
{
if($file != "." && $file != ".." && $file != "Thumbs.db"/*pesky windows, images..*/)
{
$sourcefile = $dir.$file;
$destinationfile = 'Your New Location';
if (!copy($sourcefile, $destinationfile)) {
echo "failed to copy $file...\n";
}
}
}
closedir($handle);
}
}
}
move_files("folder/");
I you want to move files, see PHP manual: copy, unlink

Categories