I'm trying to rename a file, when i call rename() function the file get gets renamed but it still gives me error "The filename uploads/09-12-2015.xls is not readable"
date_default_timezone_set('America/Chicago');
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
include 'lib/Classes/PHPExcel/IOFactory.php';
/** PHPExcel_IOFactory */
include 'connect.php';
if (isset($_POST['config']))
{
$hotelName = $_POST['shortname'];
$reportDate = $_POST['reportDate'];
$dateCol = $_POST['dateCol'];
$dateRow = $_POST['dateRow'];
$soldCol = $_POST['soldCol'];
$revCol = $_POST['revCol'];
$todayDate = date('m-d-Y');
rename("uploads/$todayDate.xls","uploads/$reportDate.xls");
echo "done";
//$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
//$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
}
Related
This question already has answers here:
Create a folder if it doesn't already exist
(21 answers)
Closed last year.
<?php
//look in directory and grab the file starttoken.php
copy('tokenmaster/starttoken.php', './starttoken.php');
$filename = './starttoken.php';
if (file_exists($filename)) {
$date = new DateTime();
//add the date
rename("./starttoken.php", "./tokendone" . $date->format('ydhis') . ".php");
//echo "Rename done";
echo "";
} else {
echo "File not found";
}
?>
//at a loss to figure out how to create a new directory to place this file into.
//revision - while I can create the new folder now, it seems that my renamed file does not get placed into the new directory, just the root, I must be doing something wrong.
<?php
$store_path = './';
$name = date('Y-m-d H-i-s');
if(!is_dir($store_path.$name)) mkdir($store_path.$name);
copy('tokenmaster/starttoken.php', './starttoken.php');
$filename = './starttoken.php';
if (file_exists($filename)) {
$date = new DateTime();
rename("./starttoken.php", "./tokendone" . $date->format('ydhis') . ".php");
}
#... your code
#build your content and store it e.g. by
file_put_contents($store_path.$name.'/'.$filename, $yourcontent);
?>
Answer as requested
<?php
$store_path = './';
$name = date('ydhis');
if(!is_dir($store_path.$name)) mkdir($store_path.$name);
copy('tokenmaster/starttoken.php', './starttoken.php');
$filename = './starttoken.php';
if (file_exists($filename)) {
$date = new DateTime();
rename("./starttoken.php", "$name/tokendone" . $date->format('ydhis') . ".php");
//rename("./starttoken.php", "$name/tokendone" . ".php");
}
?>
Please, check if this works.
<?php
$date = new DateTime();
$store_path = './' . date('Y-m-d H-i-s');
$file_path = $store_path . '/tokendone' . $date->format('ydhis') . '.php';
if(!is_dir($store_path)) mkdir($store_path);
copy('tokenmaster/starttoken.php', $file_path);
#... your code
#build your content and store it e.g. by
file_put_contents($file_path, $yourcontent);
?>
Trying to make a file structure for class, api and etc. But when I try to include a class into api/ file, problem happens... it's look like I can't give the file path's correctly. But couldn't find what is wrong.
the directory and file structure is like image below.
Insert class
class Insert extends Dbc {
public function getProjects(){
$sql = "SELECT * FROM projects";
$stmt = $this->connect()->query($sql);
while($row = $stmt->fetch()){
$row["project_name"] . "<br>";
}
}
}
autoload inc
spl_autoload_register('autoloader');
function autoloader($className){
$path = "classes/";
$extension = ".class.php";
$fullPath = $path . $className . $extension;
if (!file_exists($fullPath)) {
return false;
}
include_once $fullPath;
}
and finally, /api file projects.api
<?php
include "../inc/autoload.inc.php";
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$projects = new Insert();
echo $projects->getProjects();
?>
</body>
</html>
But here is the thing I am getting a 500 error. AND when I use try and catch on $projects = new Insert(); in api file. It says can't find the class... It means include "../inc/autoload.inc.php"; not working correctly?
I solved the issue. The mistake was I didn't include the class inside the autoload file. So it doesn't find the class.. Here is the new autoload.inc.php
spl_autoload_register('autoloader');
include "../classes/insert.class.php";
function autoloader($className){
$path = "../classes/";
$extension = ".class.php";
$fullPath = $path . $className . $extension;
if (!file_exists($fullPath)) {
return false;
}
include_once $fullPath;
}
i am using google cloud using CORE PHP to upload file using simple HTML Form But i am stuck on CloudStorageTools Class
it throws continuous following error
Fatal error: Class
'google\appengine\api\cloud_storage\CloudStorageTools' not found
For Solve that i Included
include_once __DIR__ . '/vendor/autoload.php';
//Include class
require_once "google/appengine/api/cloud_storage/CloudStorageTools.php";
use google\appengine\api\cloud_storage\CloudStorageTools;
But than it throws second error. where am i going wrong in google cloud setup.
My full code looks like
$bucket = 'bucketname';
$root_path = 'gs://' . $bucket . '/';
$options = ['gs_bucket_name' => $bucket];
$upload_url = CloudStorageTools::createUploadUrl('/upload/handler', $options);
$_url = '';
if(isset($_POST['submit']))
{
if(isset($_FILES['userfile']))
{
$name = $_FILES['userfile']['name'];
$file_size =$_FILES['userfile']['size'];
$file_tmp =$_FILES['userfile']['tmp_name'];
$original = $root_path .$name;
move_uploaded_file($file_tmp, $original);
$_url=CloudStorageTools::getImageServingUrl($original);
}
}
CodeIgniter:
A PHP Error was encountered
Severity: Warning
Message: fopen(scanner/logs/eventlogs_2018-05-06.txt): failed to open stream: No such file or directory
Filename: classes/Logger.php
Logger.php
<?php
class Logger{
private $logFile;
private $fp;
public function lfile($path) {
$this->logFile = $path;
}
public function lwrite($message){
if(!$this->fp)
$this->lopen();
$scriptName = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
$time = date('H:i:s:ms');
fwrite($this->fp, "$time ,$scriptName, $message\n");
}
private function lopen(){
$lfile = $this->logFile;
$today = date('Y-m-d');
$this->fp = fopen($lfile . '_' . $today . '.txt', 'a') or exit("Can't open $lfile!");
}
}
?>
Bear in mind that my directory is not /scanner/logs/eventlogs/ but its /application/user/views/scanner/ so I have no idea why logger is trying to fopen there... Can anyone help?
I am using this as a form to web scan!
a snippet
$log = new Logger();
$log->lfile('scanner/logs/eventlogs'); // THIS IS WHERE ERROR POPS UP
$log->lwrite('Connecting to database');
$connectionFlag = connectToDb($db);
if(!$connectionFlag)
{
$log->lwrite('Error connecting to database');
echo 'Error connecting to database';
return;
}
You should change this function (which seems to set the path for the other functions):
public function lfile($path) {
$this->logFile = $path;
}
To something like:
public function lfile($path) {
$this->logFile = FCPATH . $path;
}
This way all your paths will be from C:\xampp\htdocs\ (FCPATH example) and not depend on the current working directory where you are calling your function from.
Use the __DIR__ constant, which returns the current directory of the script.
public function lfile($path) {
$this->logFile = __DIR__ . "/" . $path; // sprintf("%s/%s", __DIR__, $path);
}
Learn more: http://php.net/manual/en/language.constants.predefined.php
I wonder whether someone may be able to help me please.
I'm using Aurigma's Image uploader software to allow users to upload images for locations they visit. The information is saved via the script shown below:
<?php
//This variable specifies relative path to the folder, where the gallery with uploaded files is located.
//Do not forget about the slash in the end of the folder name.
$galleryPath = 'UploadedFiles/';
require_once 'Includes/gallery_helper.php';
require_once 'ImageUploaderPHP/UploadHandler.class.php';
/**
* FileUploaded callback function
* #param $uploadedFile UploadedFile
*/
function onFileUploaded($uploadedFile) {
$packageFields = $uploadedFile->getPackage()->getPackageFields();
$username=$packageFields["username"];
$locationid=$packageFields["locationid"];
global $galleryPath;
$absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR;
$absThumbnailsPath = $absGalleryPath . 'Thumbnails' . DIRECTORY_SEPARATOR;
if ($uploadedFile->getPackage()->getPackageIndex() == 0 && $uploadedFile->getIndex() == 0) {
initGallery($absGalleryPath, $absThumbnailsPath, FALSE);
}
$locationfolder = $_POST['locationid'];
$locationfolder = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $locationfolder);
if (!is_dir($absGalleryPath . $locationfolder)) {
mkdir($absGalleryPath . $locationfolder, 0777);
}
$dirName = $_POST['folder'];
$dirName = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $dirName);
if (!is_dir($absGalleryPath . $dirName)) {
mkdir($absGalleryPath . $dirName, 0777);
}
$path = rtrim($dirName, '/\\') . '/';
$originalFileName = $uploadedFile->getSourceName();
$files = $uploadedFile->getConvertedFiles();
// save converter 1
$sourceFileName = getSafeFileName($absGalleryPath, $originalFileName);
$sourceFile = $files[0];
/* #var $sourceFile ConvertedFile */
if ($sourceFile) {
$sourceFile->moveTo($absGalleryPath . $sourceFileName);
}
// save converter 2
$thumbnailFileName = getSafeFileName($absThumbnailsPath, $originalFileName);
$thumbnailFile = $files[1];
/* #var $thumbnailFile ConvertedFile */
if ($thumbnailFile) {
$thumbnailFile->moveTo($absThumbnailsPath . $thumbnailFileName);
}
//Load XML file which will keep information about files (image dimensions, description, etc).
//XML is used solely for brevity. In real-life application most likely you will use database instead.
$descriptions = new DOMDocument('1.0', 'utf-8');
$descriptions->load($absGalleryPath . 'files.xml');
//Save file info.
$xmlFile = $descriptions->createElement('file');
$xmlFile->setAttribute('name', $_POST['folder'] . '/' . $originalFileName);
$xmlFile->setAttribute('source', $sourceFileName);
$xmlFile->setAttribute('size', $uploadedFile->getSourceSize());
$xmlFile->setAttribute('originalname', $originalFileName);
$xmlFile->setAttribute('thumbnail', $thumbnailFileName);
$xmlFile->setAttribute('description', $uploadedFile->getDescription());
//Add additional fields
$xmlFile->setAttribute('username', $username);
$xmlFile->setAttribute('locationid', $locationid);
$xmlFile->setAttribute('folder', $dirName);
$descriptions->documentElement->appendChild($xmlFile);
$descriptions->save($absGalleryPath . 'files.xml');
}
$uh = new UploadHandler();
$uh->setFileUploadedCallback('onFileUploaded');
$uh->processRequest();
?>
In additon to the original script I've added code that creates a folder, with it's name based on the current 'locationid'. This is shown below.
$locationfolder = $_POST['locationid'];
$locationfolder = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $locationfolder);
if (!is_dir($absGalleryPath . $locationfolder)) {
mkdir($absGalleryPath . $locationfolder, 0777);
}
What I like to incorporate, is a check that looks to see whether there is a folder already setup with the current 'locationid' value, if not create the folder. I'm ceratianly no expert in PHP, but I know that to check to see whether a file exists, the if(file exists....) can be used, but I just wondered whether someone could tell me please how I can implement this check for the folder name?
Many thanks
Chris
I think is_dir() is what you are looking for.
UPDATE:
The code you have:
if (!is_dir($absGalleryPath . $locationfolder)) {
mkdir($absGalleryPath . $locationfolder, 0777);
}
Does exactly what you want. It checks for the folder and if it does not exist then it creates one for you (with CHMOD 777). Don't see what your question is then...