PHP strpos dont works properly(Path Generator) - php

I wanted to create a function that generates an image path from an array.The function has one argument and that argument is for you to decide from which folder you want to choose images.
So I have 90 elements in an array and each element is a path.Now I don't want a .png image so I wanted to create something like this(but it didn't work for me):
public function getRandomPath($RoboticOrArenag)
{
if ($RoboticOrArenag == 'Robotic' || $RoboticOrArenag == 'robotic') {
$countofarray = count($this->allImagesRobotic);
} else if ($RoboticOrArenag == 'Arenag' || $RoboticOrArenag == 'arenag') {
$countofarray = count($this->allImagesArenag);
} else {
$RoboticOrArenag = 0;
}
$randomNumber = rand(0, $countofarray);
if ($RoboticOrArenag == 'Robotic' || $RoboticOrArenag == 'robotic') {
if($randomNumber==50){
$randomNumber--;
}
else if ($randomNumber==0){
$randomNumber++;
}
$randomNumberPath = $this->allImagesRobotic[$randomNumber];
} else if ($RoboticOrArenag == 'Arenag' || $RoboticOrArenag == 'arenag') {
if($randomNumber==33){
$randomNumber--;
}
else if ($randomNumber==0){
$randomNumber++;
}
$randomNumberPath = $this->allImagesArenag[$randomNumber];
while(strpos($randomNumberPath, '.png') == true){
$randomNumber = rand(0, $countofarray);
$randomNumberPath = $this->allImagesArenag[$randomNumber];
}
} else {
$randomNumberPath = 0;
}
$randomPath = "/img/$RoboticOrArenag/$randomNumberPath";
return $randomPath;
}

Related

File name too long while deleting image in Laravel

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);
}
}

How to repeat php "if" 20 times?

I'm trying to repeat php "if" 20 times with dh_c_01_01. How I can do it?
if ($dh_c_01_01['id'] == $a_c_a_max) {
$dh_sq_01_01 = $a_b_s_05;
} elseif ($dh_c_01_01['id'] == $a_c_a_1_b) {
$dh_sq_01_01 = $a_b_s_06;
} elseif ($dh_c_01_01['id'] == $a_c_a_2_b) {
$dh_sq_01_01 = $a_b_s_07;
} elseif ($dh_c_01_01['id'] == $a_c_a_3_b) {
$dh_sq_01_01 = $a_b_s_08;
} elseif ($dh_c_01_01['id'] == $a_c_a_4_b) {
$dh_sq_01_01 = $a_b_s_09;
} elseif ($dh_c_01_01['id'] == $a_c_a_5_b) {
$dh_sq_01_01 = $a_b_s_10;
} elseif ($dh_c_01_01['id'] == $a_c_a_6_b) {
$dh_sq_01_01 = $a_b_s_11;
} elseif ($dh_c_01_01['id'] == $a_c_a_7_b) {
$dh_sq_01_01 = $a_b_s_12;
} elseif ($dh_c_01_01['id'] == $a_c_a_8_b) {
$dh_sq_01_01 = $a_b_s_13;
} elseif ($dh_c_01_01['id'] == $a_c_a_9_b) {
$dh_sq_01_01 = $a_b_s_14;
}
if ($dh_c_02_01['id'] == $a_c_a_max) {
$dh_sq_02_01 = $a_b_s_05;
} elseif ($dh_c_02_01['id'] == $a_c_a_1_b) {
$dh_sq_02_01 = $a_b_s_06;
} elseif ($dh_c_02_01['id'] == $a_c_a_2_b) {
$dh_sq_02_01 = $a_b_s_07;
} elseif ($dh_c_02_01['id'] == $a_c_a_3_b) {
$dh_sq_02_01 = $a_b_s_08;
} elseif ($dh_c_02_01['id'] == $a_c_a_4_b) {
$dh_sq_02_01 = $a_b_s_09;
} elseif ($dh_c_02_01['id'] == $a_c_a_5_b) {
$dh_sq_02_01 = $a_b_s_10;
} elseif ($dh_c_02_01['id'] == $a_c_a_6_b) {
$dh_sq_02_01 = $a_b_s_11;
} elseif ($dh_c_02_01['id'] == $a_c_a_7_b) {
$dh_sq_02_01 = $a_b_s_12;
} elseif ($dh_c_02_01['id'] == $a_c_a_8_b) {
$dh_sq_02_01 = $a_b_s_13;
} elseif ($dh_c_02_01['id'] == $a_c_a_9_b) {
$dh_sq_02_01 = $a_b_s_14;
}
[$dh_sq_03_01, ... , $dh_sq_20_01]
up to 20 times
I cannot be sure I understand your question correctly, or assume I know what your variables are or what their values are.
Based on your question, this example uses extra variables, for loops and dynamic variables. It also assumes that each of your dynamic variables exists:
<?php
$dh_c_limit = 20; // <-- loop limit
$a_c_a_limit = 9; // <-- loop limit
$a_b_s_addition = 5; // <-- based on your question
$a_c_a_max = 0; // <-- change this to whatever '$a_c_a_max' should be!
for ($dh_i = 1; $dh_i <= $dh_c_limit; $dh_i++) {
if(${"dh_c_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"}['id'] == $a_c_a_max){ // <-- checks against '$a_c_a_max'
${"dh_sq_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"} = ${"a_b_s_".str_pad($a_c_i,2,"0",STR_PAD_LEFT)}; // <-- set the '$dh_sq_' variable to '$a_b_s_' variable
}
else{
for ($a_c_i = 1; $a_c_i <= $a_c_a_limit; $a_c_i++) {
if(${"dh_c_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"}['id'] == ${"a_c_a_".$a_c_i."_b"}){ // <-- checks against '$a_c_a_' variable
${"dh_sq_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"} = ${"a_b_s_".str_pad(($a_c_i+$a_b_s_addition),2,"0",STR_PAD_LEFT)}; // <-- set the '$dh_sq_' variable to '$a_b_s_' variable
break; // <-- break the loop
}
}
}
}
?>
That is a bad habit. Try to use switch case, or use array as container
$array = [
'id1' => 'action1',
'id2' => 'action2',
...
];
$dh_sq_02_01 = $array[$dh_c_02_01['id']];
Just and example. Hope helps

Session Userdata Type not works in particular function in Codeigniter

I use this code in home function it get the usertype and works nice.
function home()
{
$type = $this->session->userdata('type');
if($type == "admin")
{
$this->load->view('index');
}
else if($type == "QA" || "SC" || "CDC")
{
$this->load->view('aindex');
}
}
But when I use same code in jobsheet function it's not working. Code below
function jobsheet()
{
$type = $this->session->userdata('type');
$this->load->model('Ipss_model');
$var1['job'] = $this->Ipss_model->AllJobSheet();
$var2['division'] = $this->Ipss_model->AllDivision();
$var3['tItem'] = $this->Ipss_model->transactionItem();
$data = $var1 + $var2 + $var3;
if($data['job'] != NULL )
{
if($type == "SC" || "CDC")
{
$this->load->view('jobsheet',$data);
}
else if($type == "admin" || "QA" )
{
$this->load->view('jobsheetQA',$data);
}
}
else
{
$this->load->view('jobsheetEmpty',$data);
}
}
Please help me about this. Thanks in advance.
To check weather array is empty or not you can use empty(). Also change your || condition like below.
So you condition would be
if(!empty($data))
{
if($type == "SC" || $type =="CDC")
{
$this->load->view('jobsheet',$data);
}
else if($type == "admin" || $type == "QA" )
{
$this->load->view('jobsheetQA',$data);
}
}
check your array() before using it in if condition
with -:
echo "<pre>";
print_r($data);
die();

PHP Creating 3 Thumbnail Images of different Sizes from an uploaded image

I'm trying to create 3 Thumbnail Images of different sizes, from the same uploaded Image. Presently, I use the code below to create 1 thumbnail that's 150px wide.
Is there an easy way to do this instead of repeating the same code thrice for each thumbnail I need?
I'm trying to create thumbnails in 3 sizes: 750px wide, 150px wide and 70px wide.
Here's the code I use to do the Thumbnail that's 150Px wide.
Does PHP have a function to do such a thing, or is repeating the code below for each size I need my only option.
if(file_exists($thisImage)) {
$imageName = $thisImage;
$imageInfo = finfo_open(FILEINFO_MIME_TYPE);
$imageType = finfo_file($imageInfo, $imageName);
finfo_close($imageInfo);
if($imageType == 'image/pjeg' || $imageType == 'image/jpeg' || $imageType == 'image/jpg') {
$imgSource = imagecreatefromjpeg($thisImage);
} elseif ($imageType == 'image/png') {
$imgSource = imagecreatefrompng($thisImage);
} elseif ($imageType == 'image/gif') {
$imgSource = imagecreatefromgif($thisImage);
} else {
$imgSource = false;
return false;
}
if($imgSource) {
list($width,$height)=getimagesize($thisImage);
$thumbImageWidth = 150;
$thumbImageHeight = ($height/$width)*$thumbImageWidth;
$tempThumbImage = imagecreatetruecolor($thumbImageWidth,$thumbImageHeight);
if(!imagecopyresampled($tempThumbImage,$imgSource,0,0,0,0,$thumbImageWidth,$thumbImageHeight,$width,$height)) return false;
$thumbImageTarget = $thisPath.$thisName;
if(!imagejpeg($tempThumbImage,$thumbImageTarget,100)) return false;
if(!imagedestroy($imgSource)) return false;
if(!imagedestroy($tempThumbImage)) return false;
if(!unlink($thisImage)) return false;
return true;
}
} else {
return false;
}
PHP dont have that function. But you can create a function named resize($width, $heigh) and put your resize part in.
zairwolf's answer is basically correct, although not thorough.
You need to create a user defined function.
resize($thisImage, 70, $thisPath.$thisName."-thumb1.jpg");
resize($thisImage, 150, $thisPath.$thisName."-thumb2.jpg");
resize($thisImage, 750, $thisPath.$thisName."-thumb3.jpg");
function resize($imageName, $thumbImageWidth, $thumbImageTarget)
{
if(file_exists($imageName)) {
$imageInfo = finfo_open(FILEINFO_MIME_TYPE);
$imageType = finfo_file($imageInfo, $imageName);
finfo_close($imageInfo);
if($imageType == 'image/pjeg' || $imageType == 'image/jpeg' || $imageType == 'image/jpg') {
$imgSource = imagecreatefromjpeg($imageName);
} elseif ($imageType == 'image/png') {
$imgSource = imagecreatefrompng($imageName);
} elseif ($imageType == 'image/gif') {
$imgSource = imagecreatefromgif($imageName);
} else {
$imgSource = false;
return false;
}
if($imgSource) {
list($width,$height)=getimagesize($imageName);
$thumbImageHeight = ($height/$width)*$thumbImageWidth;
$tempThumbImage = imagecreatetruecolor($thumbImageWidth,$thumbImageHeight);
if(!imagecopyresampled($tempThumbImage,$imgSource,0,0,0,0,$thumbImageWidth,$thumbImageHeight,$width,$height)) return false;
if(!imagejpeg($tempThumbImage,$thumbImageTarget,100)) return false;
if(!imagedestroy($imgSource)) return false;
if(!imagedestroy($tempThumbImage)) return false;
if(!unlink($imageName)) return false;
return true;
}
} else {
return false;
}
}

jQuery/PHP File Upload Times Out Before Loading Next Page

So I am developing the following image upload script, based off an existing open-source script. It's currently viewable live here: http://images.oneightynyc.com/
Now if you take any series of regular sized images (under 5mb) and proceed to upload them, the upload process goes just fine. Uploads the files, and brings you to a page that displays the link codes to those files. However let's say you upload a few large images, like the following:
http://imaging.nikon.com/lineup/dslr/d90/img/sample/pic_005b.jpg
http://imaging.nikon.com/lineup/dslr/d90/img/sample/pic_003b.jpg
The uploads happen in the process, however the script never brings you to the uploaded page. The only way I am aware that the upload has actually taken place is if I browse to the Gallery page and see that the files are listed there.
Here is the uploader.php file which handles the upload:
<?
//ob_start();
session_start();
$auth_id=$_SESSION['userid'];
if (!$auth_id || empty($auth_id) || $auth_id==""){
$auth_id = 0;
}
require_once("config.php");
require_once("limits.php");
require_once("ftp.class.php");
require_once("func.php");
$link = mysql_connect($db_server, $db_user, $db_password) or die("Could not connect to the database.");
mysql_select_db($db_name) or die("Could not select the database.");
if ($config[Uploads] == 0) {
$msg= "<center><b><br><br><br>Uploads are temporarily disabled by the site admin</center></b>";
}
else if ($config[Uploads] == 1 && !$auth_id) {
$msg= "<center><b><br><br><br>You have to Register before you will be able to upload photos.</center></b>";
}
$query = "select count(*) as total from ftp where status=1";
$result = mysql_query($query) or die("Query failed.");
while ($row = mysql_fetch_array($result))
{
$total=$row[total];
}
if($total<=0)
{
$no_server="1";
$ftpid=0;
$url=$server_url."/images/";
}
else
{
$query = "select * from ftp where status=1 ORDER BY RAND() limit 1";
$result = mysql_query($query) or die("Query failed.");
while ($row = mysql_fetch_array($result))
{
$no_server="0";
$ftpid=$row['ftpid'];
$path=$row['name'];
$url=$row['dir'];
$host=$row['host'];
$user=$row['user'];
$pass=$row['ftppass'];
}
}
// get variables for fields on upload screen
$tos = $_POST['tos'];
$prv = $_POST['prv'];
if($prv!="1")
$prv=0;
$uploaderip = $_SERVER['REMOTE_ADDR'];
$messages="";
$msg="";
$newID="";
$FileName="";
$FileFile="";
$FileUrl="";
$FileUrlLink="";
$FiletnUrl="";
// check for blocked ip address
if ($uploaderip != "") {
$query = "select ip from blocked where ip = '$uploaderip'";
$result = mysql_query($query) or die("Query failed.");
$num_rows = mysql_num_rows($result);
if ($num_rows > 0) {
$msg= "Your IP address (".$uploaderip.") has been blocked from using this service.";
}
}
if ($config[AcceptTerms]=="1"){
if ($tos=="")
{
$msg= "You must check the box stating you agree to our terms.";
echo "<script language='javascript'>parent.upload('".$msg."','".$newID."','".$messages."','".$FileName."','".$FileFile."','".$FileUrl."','".$FileUrlLink."','".$FiletnUrl."','".$page_url."','".$server_url."','".$site_name."','".$HotLink."');</script>";
}
}
if($msg=="")
{
// check for a file
for($i=0;$i<=14;$i++)
{
$err="0";
$thefile = $_FILES['thefile'.$i];
if ($thefile['name']!="")
{
// check for valid file extension
$path_parts = pathinfo($thefile['name']);
$file_ext = strtolower($path_parts['extension']);
if ($err == "0")
{
// check for valid file type
if (!in_array_nocase($file_ext, $valid_file_ext))
{
$messages.= "|<em>".$thefile['name']."</em> is not in a valid format (".$valid_mime_types_display.")";
$err="1";
}
}
if ($err == "0") {
// check for valid image file
$imageinfo = getimagesize($_FILES['thefile0']['tmp_name']);
if(!eregi('image',$imageinfo['mime'])) {
$messages.="|". "Sorry, This is not a valid image file!";
$err="1"; } }
if ($err == "0")
{
// check for valid file size
if ($thefile['size'] > ($max_file_size_b))
{
$filesizemb =($thefile['size']/1048576);
$filesizemb = number_format($filesizemb, 3);
$messages.="Sorry but this image size is ".$filesizemb." MB which is bigger than the max allowed file size of ".$max_file_size_mb." MB.";
$err="1";
}
}
// save the file, if no error messages
if ($err == "0")
{
// replace special chars with spaces
$thefile['name'] = eregi_replace("[^a-z0-9.]", " ", $thefile['name']);
// Replace multiple spaces with one space
$thefile['name'] = ereg_replace(' +', ' ', $thefile['name']);
// Replace spaces with underscore
$thefile['name'] = str_replace(' ', '_', $thefile['name']);
// Replace hyphens with underscore
$thefile['name'] = str_replace('-', '_', $thefile['name']);
// Replace multiple underscores with one underscore
$thefile['name'] = ereg_replace('_+', '_', $thefile['name']);
$path_parts = pathinfo($thefile['name']);
// if php < 5.2
if(!isset($path_parts['filename'])){
$path_parts['filename'] = substr($path_parts['basename'], 0,strpos($path_parts['basename'],'.'));
}
$thefile['name'] = strpos($path_parts['filename'], '.');
$thefile['name'] = substr($path_parts['filename'], 0, 22); // limit file name length to 22 chars from the beginning
$thefile['name'] = $thefile['name'] . "." . strtolower($path_parts['extension']);
// Generate prefix to add to file name
$prefix = rand(99,999);
// Add prefix to file name
$newFileName = $prefix . $thefile['name'];
// SAVE THE PICTURE
$FileName.="|". newImageName($thefile['name']);
$FileFile.="|". $server_dir . $newFileName;
$newFile = $server_dir . $newFileName;
$newFileUrl = $url . $newFileName;
$FileUrl.="|". $url . $newFileName;
$newFileUrlLink = $server_save_directory . $newFileName;
$FileUrlLink.="|". $newFileName;
if (in_array_nocase($file_ext, $valid_file_ext))
{
$lx = 3;
if ($file_ext == "jpeg") {
$lx = 4; }
$tnFileName = substr($newFileName, 0, strlen($newFileName) - $lx) . "jpg";
$tnFileName = str_replace('.', '_tn.', $tnFileName);
$tnFile = $server_dir . $tnFileName;
$FiletnUrl.="|". $url . $tnFileName;
$tnFileUrl = $url . $tnFileName;
}
else
{
$tnFileName = "";
$tnFile = "";
$tnFileUrl = "";
}
$filesize = $thefile['size'];
$newID = "";
if (!#copy($thefile['tmp_name'], $newFile))
{
$messages.="|". "Please check site settings in admin panel and set proper value for server local path.<br><br>Also please make sure the images folder is chmodded to 0777";
}
else
{
// add to database
if($auth_id)
$uid=$auth_id;
else $uid=0;
//ftpupload($host,$user,$pass,$path."/".$dir."/".$newFileName,$newFileUrl);
//ftpupload
if($no_server=="0")
{
$ftp =& new FTP();
if ($ftp->connect($host)) {
if ($ftp->login($user,$pass)) {
$ftp->chdir($path);
$ftp->put($newFileName,$newFile);
}
}
// unlink($newFile);
}
//ftpupload
$date_add=time();
$query = "INSERT INTO images (prv,ftpid,userid,filename, tn_filename, filepath, ip, filesize,added) VALUES ($prv,$ftpid,$uid,'$newFileName', '$tnFileName', '$url', '$uploaderip', $filesize,$date_add)";
mysql_query($query) or die("Database entry failed.");
$newID.="|". mysql_insert_id();
}
if ($file_ext == "jpeg" ||$file_ext == "jpg" || $file_ext == "png" || $file_ext == "gif" || $file_ext == "bmp")
{
if ($file_ext == "jpg")
{
$source_id = imagecreatefromjpeg($newFile);
}
if ($file_ext == "jpeg")
{
$source_id = imagecreatefromjpeg($newFile);
}
elseif ($file_ext == "png")
{
$source_id = imagecreatefrompng($newFile);
}
elseif ($file_ext == "gif")
{
$source_id = imagecreatefromgif($newFile);
}
elseif ($file_ext == "bmp")
{
$source_id = ImageCreateFromBMP($newFile);
}
$true_width = imagesx($source_id);
$true_height = imagesy($source_id);
}
}
}
}
mysql_close($link);
// create URL links to display to user
$showURL1 = false; // image on hosted page - image only
$showURL2 = false; // direct link to file - all
$showURL3 = false; // HTML for img - image only
$showURL4 = false; // [img][/img] tags - image only
$showURL5 = false; // thumbnail pic - image only
// determine flags
$showURL2 = true;
if ($file_ext == "jpg" || $file_ext == "jpeg"|| $file_ext == "gif" || $file_ext == "png" || $file_ext == "bmp") {
$showURL1 = true;
$showURL3 = true;
$showURL4 = true;
}
if ($file_ext == "jpg" || $file_ext == "gif" || $file_ext == "png"|| $file_ext == "jpeg" || $file_ext == "bmp") {
$showURL5 = true;
}
echo "<script language='javascript'>parent.upload('".$msg."','".$newID."','".$messages."','".$FileName."','".$FileFile."','".$FileUrl."','".$FileUrlLink."','".$FiletnUrl."','".$page_url."','".$server_url."','".$site_name."','".$HotLink."');</script>";
}
else
{
echo "<script language='javascript'>parent.uploaderror('".$msg."');</script>";
exit;
}
function newImageName($fname) {
$timestamp = time();
$new_image_file_ext = substr($fname, strlen($fname) - 3, strlen($fname));
if ($new_image_file_ext == "peg") {
$ext = ".jpg";
} else {
$ext = "." . $new_image_file_ext;
}
$newfilename = randString() . substr($timestamp, strlen(timestamp) - 4, strlen(timestamp)) . $ext;
return $newfilename;
}
function randString() {
$newstring="";
while(strlen($newstring) < 3) {
$randnum = mt_rand(0,61);
if ($randnum < 10) {
$newstring .= chr($randnum + 48);
} elseif ($randnum < 36) {
$newstring .= chr($randnum + 55);
} else {
$newstring .= chr($randnum + 61);
}
}
return $newstring;
}
function in_array_nocase($item, $array) {
$item = &strtoupper($item);
foreach($array as $element) {
if ($item == strtoupper($element)) {
return true;
}
}
return false;
}
?>
And the upload.js script which takes care of producing the uploaded page:
var cp = new cpaint();
cp.set_transfer_mode('get');
cp.set_response_type('xml');
cp.set_debug(1);
function uploaderror(msg)
{
alert(msg);
}
function showfile()
{
var countfld=1;
countfld=document.getElementById("countfld").value+countfld;
fld=countfld.length;
if(fld>14)
{
alert("Sorry, i can upload max 15 files at once.");
return false;
}
else
{
document.getElementById("f"+fld).style.display="block";
document.getElementById("countfld").value=countfld;
}
var file=document.getElementById("f"+fld).value;
if(file=="")
{
msg="Please fill this field.";
alert(msg);
document.getElementById("f"+fld).focus();
return false;
}
}
function showfileux()
{
var countfld=1;
countfld=document.getElementById("countfldu").value+countfld;
fld=countfld.length;
if(fld>14)
{
alert("Sorry, i can upload max 15 files at once.");
return false;
}
else
{
document.getElementById("u"+fld).style.display="block";
document.getElementById("countfldu").value=countfld;
}
}
function showfileu()
{
var countfld=1;
countfld=document.getElementById("countfldu").value+countfld;
fld=countfld.length;
fldx=fld-1;
fldxx=fld.value;
if(fldxx=="")
{
msg="Email Address cannot be left empty.";
alert(msg);
document.getElementById("u"+fldxx).select();
document.getElementById("u"+fldxx).focus();
return false;
}
if(fld>14)
{
alert("Sorry, i can upload max 15 files at once.");
return false;
}
else
{
document.getElementById("u"+fld).style.display="block";
document.getElementById("countfldu").value=countfld;
}
}
function uploadfile(id)
{
if(document.getElementById(id).value==1)
{
document.getElementById("showurl").style.display="none";
document.getElementById("showfl").style.display="block";
return true;
}
if(document.getElementById(id).value==2)
{
document.getElementById("showfl").style.display="none";
document.getElementById("showurl").style.display="block";
return true;
}
document.getElementById("countfldu").value="0";
document.getElementById("countfld").value="0";
}
function show_loading()
{
document.getElementById('loading').style.display = "block";
document.getElementById('newupload').submit;
document.getElementById('submit').disabled = true;
// return true;
}
function show_loading1()
{
document.getElementById('loading1').style.display = "block";
document.getElementById('newupload1').submit;
document.getElementById('submit').disabled = true;
}
function upload(msg,newID,messages,FileName,FileFile,FileUrl,FileUrlLink,FiletnUrl,page_url,server_url,site_name,HotLink)
{
var html='<div id="wrapper"><div style="width:760px;"><center><FONT SIZE="4" COLOR="#00A4B7">Photo Links</FONT></h4><br></center><span class="body"><form name="uploadresults" action="uploademail.php" method="post">';
if(newID)
{
html=html+'<input type="hidden" name="idx[]" value="'+newID+'">';
}
if(msg)
{
var getmsg = msg.split("|");
for(i=0;i<getmsg.length;i++)
{
if(getmsg[i] && getmsg[i]!="on")
html=html+'<span style="font-weight: bold; color: red;">'+getmsg[i]+'</span><br>';
}
}
html=html+'<br><center>';
if(messages)
{
var getmessages = messages.split("|");
for(i=0;i<getmessages.length;i++)
{
if(getmessages[i] && getmessages[i]!="on")
html=html+'<span style="font-weight: bold; color: red;">'+getmessages[i]+'</span>';
}
html=html+'</center>';
}
if(FileName)
{
var getFileName = FileName.split("|");
var getFileFile = FileFile.split("|");
var getFileUrl = FileUrl.split("|");
var getFileUrlLink = FileUrlLink.split("|");
var getFiletnUrl = FiletnUrl.split("|");
var getHotLink = HotLink.split("|");
for(i=0;i<getFileName.length;i++)
{
if(getFileName[i] && getFileName[i]!="on") {
html=html+'<center><br><img src="'+getFileUrl[i]+'" style="max-width: 550px;"" /><br><br>';
html=html+'<strong>Link to add tags and delete the photo <br><div align="center"><textarea name="url1[]" cols="80" rows="1" READONLY onfocus="javascript: this.select()">'+server_url+'/view2.php?filename='+getFileUrlLink[i]+'
Let me know what you think is causing this error, as this is the final step I need to fix.
I've had similar issue with creating excel files from large data bases. What it boils down to is that the PHP script exceeds the servers set time limit. There are multiple ways to delay/extend this from built in PHP functions, some or all may be used. I personally had use a combination of the ability with AJAX to allow it run in the backgroun and then redirect that page.
Here is the documentation on how to delay/extend it:
http://php.net/manual/en/function.set-time-limit.php
Here is the documentation on how check for a time out as well:
http://php.net/manual/en/function.connection-timeout.php
If you end up going the AJAX route as I did, I highly recommend going the jQuery route instead of vanilla JS.

Categories