I tried to store the image in DB along with some other data in another table ,but I get
"message": "Undefined variable $filenameA"
//for picture refrences
if (!empty($request->picture_a)) {
$fileA = $request->file('picture_a');
$extensionA = $fileA->getClientOriginalExtension();
$filenameA = 'picture_a' . '_' . time() . '.' . $extensionA;
$fileA->storeAs('uploads/picture_refrences', $filenameA);
$data['picture_a'] = 'public/uploads/' . $filenameA;
}
if (!empty($request->picture_b)) {
$fileB = $request->file('picture_b');
$extensionB = $fileB->getClientOriginalExtension();
$filenameB = 'picture_b' . '_' . time() . '.' . $extensionB;
$fileB->storeAs('uploads/picture_refrences', $filenameB);
$data['picture_b'] = 'public/uploads/' . $filenameB;
}
if (!empty($request->picture_c)) {
$fileC = $request->file('picture_c');
$extensionC = $fileC->getClientOriginalExtension();
$filenameC = 'picture_c' . '_' . time() . '.' . $extensionC;
$fileC->storeAs('uploads/picture_refrences', $filenameC);
$data['picture_c'] = 'public/uploads/' . $filenameC;
}
This is what I used and it was working when I used it to store in public directory now since I tried to store in storage it does not upload images
enter image description here
you can use this code, it is working and tested
$image = $request->file('picture_a');
if (isset($image))
{
$currentDate = Carbon::now()->toDateString();
$imageName = $currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
if (!file_exists('uploads/photos')){
mkdir('uploads/photos',0777,true);
}
$image->move('uploads/photos',$imageName);
}
Related
The below function works in my create method and is a solution from another post of mine. I cannot seem to get to work in my update method--a Call to a member function storeAs() on string error keeps persisting. The error screen highlights the foreach loop.
edit.php
public function updateTentry($id)
{
$tentry = Tentry::find($id);
$images = [
'image_plant_general',
'image_plant_closeup',
'image_fruit_in_plant',
'image_fruit_in_plant_closeup',
'image_fruit_in_harvest_single',
'image_fruit_in_harvest_group'
];
$data = [
'trial_id' => $this->trial->id,
'pt_plant_vigor' => $this->pt_plant_vigor,
'pt_plant_color' => $this->pt_plant_color,
'pt_plant_growth' => $this->pt_plant_growth,
...
// more variables
];
foreach ($images as $image) {
$data[$image] = $this->{$image}?->storeAs($this->evaluation->id, $this->trial->id . '_' . $this->tentry->id . '_' . $image . '_' . Carbon::now()->toDateString() . '.' . $this->{$image}->getClientOriginalExtension(), 'trial-entry-photos');
}
$tentry->update($data);
session()->flash('success', 'Tentry Updated Successfully');
return redirect()->route('tentry.edit', [$this->evaluation->id, $this->trial->id, $tentry->id]);
}
The store and storeAs functions are available on UploadedFile.
$request->file() returns an instance of IlluminateHttp\UploadedFile on which store and storeAs() methods exists.
So you need to change the following
foreach ($images as $image) {
$data[$image] = $this->{$image}?->storeAs($this->evaluation->id, $this->trial->id . '_' . $this->tentry->id . '_' . $image . '_' . Carbon::now()->toDateString() . '.' . $this->{$image}->getClientOriginalExtension(), 'trial-entry-photos');
}
to
foreach ($images as $image) {
if(request()->hasFile($image) && request()->file($image)->isValid()) {
$uploadedFile = request()->file($image);
$data[$image] = $uploadedFile->storeAs(
/** path where the uploaded file is to be stored */
$this->evaluation->id,
/** Filename by which the uploaded file must be stored */
$this->trial->id . '_' . $this->tentry->id . '_' . $image . '_' . Carbon::now()->toDateString() . '.' . $uploadedFile->getClientOriginalExtension(),
/** name of one of the disks defined in the config/filesystems.php */
'trial-entry-photos');
}
}
Hi I have security cam that uploads via ftp to my server and I want to show last images as slideshow but I can't manage it to work. I have this code
$base_path = 'wp-content/uploads/camer/10.121.0.202';
$latest_date_folder = scandir($base_path, SCANDIR_SORT_DESCENDING);
$latest_folder = scandir($base_path . "/" . $latest_date_folder[0], SCANDIR_SORT_DESCENDING);
$directory = '../" . $base_path . "/" . $latest_date_folder[0] . "/" . $latest_folder[0] . ';
try {
echo '<div id="myslides">';
foreach ( new DirectoryIterator($directory) as $item ) {
if ($item->isFile()) {
$path = $directory . '/' . $item;
echo '<img src="' . $path . '"/>';
}
}
echo '</div>';
}
catch(Exception $e) {
echo 'No images found for this slideshow.<br />';
}
?>
I am still getting 'No images found for this slideshow.'
But when I try this code
$latest1_date_folder = scandir($base1_path, SCANDIR_SORT_DESCENDING);
$latest1_folder = scandir($base1_path . "/" . $latest1_date_folder[0], SCANDIR_SORT_DESCENDING);
$latest1_file = scandir($base1_path . "/" . $latest1_date_folder[0] . "/" . $latest1_folder[0] , SCANDIR_SORT_DESCENDING);
echo "<img src='../" . $base1_path . "/" . $latest1_date_folder[0] . "/" . $latest1_folder[0] . "/" . $latest1_file[0] . "' />";
It displays last image normally. What am I doing wrong? Thanks a lot.
I am using wordpress plugin phpcode snippets
your directory variable is wrong!
you should write like this
$directory = '../'. $base_path . "/" . $latest_date_folder[0] . "/" . $latest_folder[0];
On desktop, the upload works correctly, but when I try to upload from a mobile device, it says that the extension is outside the array, and print returns octet-stream, any thoughts? I can't see what could be wrong. I have tried many audio extensions, checked MIME and tested with a few smartphones, no success.
if (isset($_POST['submit_atrack'])) {
$fileinfo=PATHINFO($_FILES["audio"]["name"]);
$newFilename=$fileinfo['filename'] . uniqid($usr_idLogged) . "." . $fileinfo['extension'];
move_uploaded_file($_FILES["audio"]["tmp_name"],"atracks/" . $newFilename);
$location = 'atracks/' . $newFilename;
$formatos = array('audio/mpeg', 'audio/ogg', 'audio/mp3', 'audio/*', 'audio/wav');
$tamanho_max = 500000000; //500 mb
if (empty($_FILES['audio']['name']))
{
$upload_empty = urlencode("empty");
header("Location:playlist?newtrack=".$upload_empty . "&playlist=" . $selectedPlaylist);
exit;
}
else if (!in_array($_FILES['audio']['type'], $formatos))
{
$upload_incorrectformat = urlencode("incorrectext");
header("Location:playlist?newtrack=".$upload_incorrectformat . "&playlist=" . $selectedPlaylist);
exit;
}
else if (in_array($_FILES['audio']['type'], $formatos))
{
if($_FILES['audio']['size'] >= $tamanho_max)
{
$upload_maxsize = urlencode("maxsizereached");
header("Location:playlist?newtrack=".$upload_maxsize . "&playlist=" . $selectedPlaylist);
exit;
}
else
{
$atitle = mysqli_real_escape_string($connection, $_POST['atitle']);
$atag = mysqli_real_escape_string($connection, $_POST['atag']);
$atagcolor = mysqli_real_escape_string($connection, $_POST['atagcolor']);
mysqli_query($connection,"INSERT INTO tbl_users_atrack (atrack_title, atrack_path, atrack_tag, atrack_tagColor, atrack_playlistId) VALUES ('$atitle', '$location', '$atag', '$atagcolor', '$selectedPlaylist')");
$upload_success = urlencode("success");
header("Location:playlist?newtrack=" . $upload_success . "&playlist=" . $selectedPlaylist);
exit;
}
}
}
My controller code where i store the file name into a database table and also moving the file to a folder.
The issue is that i am storing the original name of a file in database table, in contrast i am moving files with uniqueid() and time() . It will arise issues in future. because in database table file name and moved file are with different names.
What i want is to store the file name into database table and move the file to folder with with uniqueid() and time().
Code :
if(Input::hasFile('profile_pic')){
$pic = Input::file('profile_pic');
$mobile->photo1 = $pic[0]->getClientOriginalName();
$mobile->photo2 = $pic[1]->getClientOriginalName();
$mobile->photo3 = $pic[2]->getClientOriginalName();
$mobile->photo4 = $pic[3]->getClientOriginalName();
$mobile->photo5 = $pic[4]->getClientOriginalName();
foreach ($pic as $k=>$file){
if(!empty($file)){
$file->move(public_path() . '/uploads/', time() . uniqid() . '-' . $k . '-laptop');
}
}
}
You will need to store the destination path into a variable and then reuse it to move the file and to store its value in the database
if(Input::hasFile('profile_pic')){
$pic = Input::file('profile_pic');
$mobile->photo1 = $pic[0]->getClientOriginalName();
$mobile->photo2 = $pic[1]->getClientOriginalName();
$mobile->photo3 = $pic[2]->getClientOriginalName();
$mobile->photo4 = $pic[3]->getClientOriginalName();
$mobile->photo5 = $pic[4]->getClientOriginalName();
foreach ($pic as $k=>$file){
if(!empty($file)){
$destinationPath = public_path() . '/uploads/', time() . uniqid() . '-' . $k . '-laptop';
$file->move($destinationPath);
// and you may store the path here to the database
// like yourObject->filePath = $destinationPath;
// yourObject->save();
}
}
}
I am creating a plugin for a WP site and I have to make an initial import of a huge amount of files from another directory. (The plugin is working well, etc etc.) I want to run the import using command line. Everything is OK if executing on local (Windows), but when I execute from Linux bash the defined constants are not evaluated but printed as a string.
define("ASCOR_RECORDINGS_TBL", $wpdb->prefix . "recordings");
Is there any setting I have to make to enable evaluation of PHP defined constants? My website is running on Dreamhost server.
When I execute the folowing line:
[myserver]$ /usr/local/php5/bin/php /home/path/to/import.php
I get:
WordPress database error Table 'db_ro.ASCOR_RECORDINGS_TBL' doesn't exist for query INSERT INTO `ASCOR_RECORDINGS_TBL` ...........
The content of the file I execute:
<?php
require_once dirname(dirname(dirname(dirname(__FILE__)))) . "/wp-load.php";
require_once "class/AscorDbHelper.php";
$origin = realpath("/path/to/files");
$upload_dir = wp_upload_dir();
$subdir = $upload_dir['subdir'];
mkdir(ASCOR_UPLOAD_DIR . "/" . $subdir, 777, true);
require_once $origin . "/classes/PConfFiles.inc.php";
$db = new AscorDbHelper();
$cf = new PConfFiles('/^PC_([0-9\-]+)_(.*)\.([a-z0-9]{2,4})$/i', $origin);
$list = $cf->getFilesList();
$catPC = $db->addCategory("Special");
$catOther = $db->addCategory("Other");
if($list){
$pc = $db->addAuthor("Ciprian V.");
foreach($list as $rec){
$fileUrl = $subdir . "/" . $rec[0];
$desc = str_replace("-", " ", $rec[2]);
copy(realpath($origin . "/" . $rec[0]), ASCOR_UPLOAD_DIR . "/" . $fileUrl );
$db->addRecording($fileUrl, $catPC->id, $pc->id, $desc, null, $rec[1]);
echo "Added: " . $rec[0] . "\n";
}
}
$cf = new PConfFiles('/^([0-9\-]+)\_([^\_]+)_(.*)\.([a-z0-9]{2,4})$/i', $origin);
$list = $cf->getFilesList();
if($list){
foreach($list as $rec){
$authorName = str_replace("-", " ", $rec[2]);
$date = $rec[1];
$desc = str_replace("-", " ", $rec[3]);
$fileUrl = $subdir . "/" . $rec[0];
$authorId = $db->getAuthorIdOrSaveIt($authorName);
copy(realpath($origin . "/" . $rec[0]), ASCOR_UPLOAD_DIR . "/" . $fileUrl );
$db->addRecording($fileUrl, $catOther->id, $authorId, $desc, null, $date);
echo "Added: " . $rec[0] . "\n";
}
}
echo "done";
The constants are defined in the main file of the plugin:
define("ASCOR_RECORDINGS_TBL", $wpdb->prefix . "recordings");
define("ASCOR_RECORDINGS_AUTHORS_TBL", $wpdb->prefix . "recordings_authors");
define("ASCOR_RECORDINGS_CATEGORIES_TBL", $wpdb->prefix . "recordings_categories");
define("ASCOR_RECORDS_PER_PAGE", 50);
define("ASCOR_EXTEND_DIR", dirname(__FILE__));
define("ASCOR_EXTEND_URL", plugins_url("", __FILE__));
define("ASCOR_NOTIFY_UPDATED", "updated");
define("ASCOR_NOTIFY_ERROR", "error");
define("ASCOR_UPLOAD_DIR", ABSPATH . "/wp-content/uploads/recordings");
Is the file loaded? Introduce a parse error into it and see if it fails. And try it on Windows too.