Adding Files After mkdir - php

I can't quite figure out how to add files from a sample directory that I have. I would appreciate some help. I have successfully created a new directory when ever a user inputs their username, but the contents are empty.
Edit: I've created the desired directory structure, just need to include/copy over the pages that I have in the sample template.
<?php
$fileName = "/associate/sample/";
$Name = $_POST['name'];
$thisdir = getcwd();
$folderPath = $thisdir . '/' . $Name;
mkdir($folderPath);
chmod($folderPath, 0777);
file_put_contents (realpath("$fileName"), associate/$Name);
?>

You were close, just not quite doing it right. Look this over, and compare with what you have to see what I've done differently
$folder = "/associate/";
$name = 'Joshua';
$thisdir = getcwd();
$folderPath = $thisdir . $folder . $name;
$filename = $name.'.file';
if(!file_exists($folderPath)){
mkdir($folderPath);
chmod($folderPath,0777);
}
$textToWrite = 'this is some text';
file_put_contents(realpath($folderPath).'/'.$filename, $textToWrite);

Related

Delete image after upload new one

I'm working with laravel 5.4 I have a form which I can upload my logoimage and in update function I have this code:
//Save logo
if ($request->hasFile('logo')) {
$avatar = $request->file('logo');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('avatars/logos/');
$request->file('logo')->move($location, $filename);
$oldFilename = $general_Settings->logo;
$general_Settings->logo = $filename;
Storage::delete($oldFilename);
}
$general_Settings->save();
for updating my image which is work but as you see I have Storage::delete($oldFilename); this part doesn't work and just keep the old image.
what do you think is issue of that?
Solved:
The issue was Filesystem.php I made my local root set to 'root' => public_path('avatars/'), and changed all my functions in my app because no way to save images in sub-folders and delete them just can save in sub-folders.
then my update function become like this:
if ($request->hasFile('logo')) {
$avatar = $request->file('logo');
$filename = 'sitelogo' . '-' . time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('avatars/');
$request->file('logo')->move($location, $filename);
$general_Settings->logo = $filename;
}
$general_Settings->save();
I hope this help someone.
Since, You have stored logo file name only not full path of file,
You need to give full path from public folder to delete image. Change delete line as:
if ($request->hasFile('logo')) {
$avatar = $request->file('logo');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('avatars/logos/');
$request->file('logo')->move($location, $filename);
$oldFilename = $general_Settings->logo;
$file_path = "avatars/logos/".$oldFilename;
$general_Settings->logo = $filename;
Storage::delete($file_path);
}
$general_Settings->save();
This might work.

How to check Image name in folder and rename it?

I have a csv file with image name and Prefix for image and I want to add prefix in that image name and rename it and move it to another directory.
<?php
$fullpath = '/var/www/html/john/Source/01-00228.jpg';
$additional = '3D_Perception_';
while (file_exists($fullpath)) {
$info = pathinfo($fullpath);
$fullpath = $info['dirname'] . '/' . $additional
. $info['filename']
. '.' . $info['extension'];
echo $fullpath ;
}
?>
Here Image file is store in Source Directory I want to rename it with some prefix and move it other directory like Destination
Please help me out to find solution for this.
<?php
$source_directory = '/var/www/html/john/Source/';
$dest_directory = '/var/www/html/john/Source/'; // Assuming you'll change it
$filename = '01-00228.jpg';
$prefix = '3D_Perception_';
$file = $source_directory . $filename;
$dest_file = $dest_directory . $prefix . $filename;
if (file_exists($file)) {
if (copy($file, $dest_file)) {
unlink($file); // Deleting source file.
var_dump(pathinfo($dest_file)); // Dump dest file infos, replace it with wathever you want to do.
} else {
// if copy doesn't work, do something.
}
}
?>
Try this, and take care about my comments. ;)

file_get_contents() cannot read PHP tags <?php ?>

I am using cakePHP and got stuck with this riddle.
Time ago i made function that inserts custom routes dynamically in file using file_get_contents() and string replace functions.
$filename = "301routes.php";
$path = SERVER_PUBLIC_PATH . 'app' . DS . 'config';
$add_data = "Router::connect('".$old."', array('controller'=>'index_router', 'action'=>'redirect_301','".$new."'));\n";
$file_data = file_get_contents($path . DS . $filename);
$file_data = str_replace("<?php\n","<?php\n".$add_data,$file_data);
file_put_contents($path . DS . $filename, $file_data);
this worked, but now i tried to put exactly the same function in other project and its working only when there is no PHP tag. Problem is with file_get_contents() function becouse it reads file well when content is:
// routes here
Router::connect('/articles/category/en/test', array('controller'=>'index_router', 'action'=>'redirect_301','/articles/category/en/test-old'));
but when i change beginning like this
<?php
Router::connect('/articles/category/en/test', array('controller'=>'index_router', 'action'=>'redirect_301','/articles/category/en/test-old'));
?>
file_get_contents() returns only part of file:
string(154) "'index_router', 'action'=>'redirect_301','/articles/category/en/test-old')); ?>"
Can someone help me with this?
To run php codes and assign to $file_data variable,
ob_start();
$file_data = file_get_contents($path . DS . $filename);
ob_end_flush();
Try: Create new file 301routes.php in config directory then read write permission of file.
$filename = "301routes.php";
$path = __DIR__;
$add_data = "Router::connect('".$old."', array('controller'=>'index_router', 'action'=>'redirect_301','".$new."'));\n";
$file_data = file_get_contents($path . DS . $filename);
$file_data = str_replace("<?php\n","<?php\n".$add_data,$file_data);
file_put_contents($path . DS . $filename, $file_data);

Uploading a csv file in PHP. Where is the error in the code

<?php
require '../config.php';
// Edit upload location here
$result = 0;
$name = mysql_real_escape_string($_FILES['myfile']['name']);
$path = csv;
$ext = 'csv';
$md5 = md5($name);
$target_path = $path . '\\' . $md5 . '.' . $ext;
if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
$result = 1;
}
sleep(1);
?>
It won't upload any files such as with the file names that contain brackets, etc.
Don't do:
$name = mysql_real_escape_string($_FILES['myfile']['name']);
Do:
$name = $_FILES['myfile']['name'];
you are getting the MD5 of $name so no reason to clean it as it will produce a 32-char hex string which will not contain any special characters regardless. If a filename contains special chars and you escape using the above the MD5 will completely change.
The error is likely here:
$path = csv;
Here PHP is looking for a constant with name csv unless you have defined that, it is going to return null, so your $target_path is not being built correctly.
Just one more thing to try is use the pre defined DIRECTORY_SEPERATOR constant while building your $target path so...
$target_path = $path . DIRECTORY_SEPERATOR . $md5 . '.' . $ext;

Renaming file on upload

I'm trying to upload an image to the server using a PHP script. It works fine, till I want to change the filename using a variable in the name.
I want to rename the file to the logged in users username_filename.extension, but it just skips the username variable so the name becomes: _filename.extension.
<?php
session_start();
include "./global.php";
$res = mysql_query("SELECT * FROM users WHERE id='".$_SESSION['uid']."'");
$row = mysql_fetch_assoc($res);
$username = $row['username'];
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$ext = explode('.',$_FILES['Filedata']['name']);
$extension = $ext[1];
$newname = '/var/www/picturebox/albums/' . $username . '_' . $ext[0] . '.' . $ext[1];
move_uploaded_file($tempFile,$newname);
}
?>
And the filename becomes _filename.extension
Anyone knows what to do?
Just found out that it works fine in IE, but now in Chrome. Any suggestions on a solution?

Categories