can't upload multiple images iin zend freamework on server - php

i have a problem with upload mutilply files using zend framework on server
actually my code works correctly on localhost but on remote server it gives me application error message
my host is ipage.com
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->setDestination('projects\\'.$_pId);
// $_pid is my project folder where all files related to it uploded
$files = $upload->getFileInfo();
$i = 1;$g = 1;
foreach ($files as $file => $info)
{
// i have three kinds of images
// innerfinishingphotos_ images which can be more than 1 file eg :innerfinishingphotos_1, innerfinishingphotos_2, innerfinishingphotos_3.
// outerfinishingphotos_ images which can be more than 1 file eg :outerfinishingphotos_1, outerfinishingphotos_2, outerfinishingphotos_3.
// _main_photo image an image.
// here i made if statements to determine which file came from which input file
if( $info == $files["innerfinishingphotos_".$i] && $info["name"] == $files["innerfinishingphotos_".$i]["name"] && !empty( $info["name"] ) )
{
$filename = "inner_finishing".$_pId.uniqid().$files["innerfinishingphotos_".$i]["name"];
$upload->addFilter('Rename', $filename, $files["innerfinishingphotos_".$i]);
$photodata = Array ("project_id"=> $_pId, "photo_link"=> "/projects/".$_pId."/".$filename, "photo_name"=> "inner_finishing");
$projectModel->addInProjectGalary($photodata);
$i++;
}
else if( $info == $files["outerfinishingphotos_".$g] && $info["name"] == $files["outerfinishingphotos_".$g]["name"] &&!empty( $info["name"] ) )
{
$filename = "outer_finishing".$_pId.uniqid().$files["outerfinishingphotos_".$g]["name"];
$upload->addFilter('Rename', $filename, $files["outerfinishingphotos_".$g]);
$photodata = Array ("project_id"=> $_pId, "photo_link"=> "/projects/".$_pId."/".$filename, "photo_name"=> "outer_finishing");
$projectModel->addInProjectGalary($photodata);
$g++;
}
else if ($info == $files["_main_photo"] && !empty( $info["name"] ))
{
$filename = "main_photo".$_pId.uniqid().$files["_main_photo"]["name"];
$upload->addFilter('Rename', $filename, $files["_main_photo"]);
$photodata = Array ("project_id"=> $_pId, "photo_link"=> "/projects/".$_pId."/".$filename, "photo_name"=> "project_photo");
$projectModel->addInProjectGalary($photodata);
}
//then i receive the image
if($upload->isValid($file))
{
try {
$upload->receive($file);
}
catch (Exception $e) {
echo "upload exteption";
}
}
}
i tested this code and i works correctly on localhost and all images uploaded and their data entered my database
but on my remote host 'ipage.com' not work.
please guys help me

after many hours trying, i solved the problem , it wase in this line
$upload->setDestination('projects\\'.$_pId);
changing it to
$upload->setDestination('projects/'.$_pId);
thank you mr. Tim Fountain

Related

Upload fails "move uploaded file"

First off, the upload folder is given 777, and my old upload script works, so the server accepts files. How ever this is a new destination.
I use krajee bootstrap upload to send the files. And I receive a Jason response. The error seems to be around move uploaded file. I bet it's a simple error from my side, but I can't see it.
<?php
if (empty($_FILES['filer42'])) {
echo json_encode(['error'=>'No files found for upload.']);
// or you can throw an exception
return; // terminate
}
// get the files posted
$images = $_FILES['filer42'];
// a flag to see if everything is ok
$success = null;
// file paths to store
$paths= [];
// get file names
$filenames = $images['name'];
// loop and process files
for($i=0; $i < count($filenames); $i++){
$ext = explode('.', basename($filenames[$i]));
$target = "uploads" . DIRECTORY_SEPARATOR . md5(uniqid()) . "." . array_pop($ext);
if(move_uploaded_file($_FILES["filer42"]["tmp_name"][$i], $target)) {
$success = true;
$paths[] = $target;
} else {
$success = false;
break;
}
}
// check and process based on successful status
if ($success === true) {.
$output = [];
$output = ['uploaded' => $paths];
} elseif ($success === false) {
$output = ['error'=>'Error while uploading images. Contact the system administrator'];
// delete any uploaded files
foreach ($paths as $file) {
unlink($file);
}
} else {
$output = ['error'=>'No files were processed.'];
}
// return a json encoded response for plugin to process successfully
echo json_encode($output);
?>
I think field name is the issue. Because you are getting image name with filer42 and upload time, you are using pictures.
Please change
$_FILES["pictures"]["tmp_name"][$i]
to
$_FILES["filer42"]["tmp_name"][$i]
And check now, Hope it will work. Let me know if you still get issue.
The error is not in this script but in the post.
I was using <input id="filer42" name="filer42" type="file">
but it have to be <input id="filer42" name="filer42[]" type="file" multiple>
as the script seems to need an arrey.
It works just fine now.

Renaming filenames and immediately reading files having issues in php

I have pdf files which are report cards of students.The report card names format is <student full name(which can have spaces)><space><studentID>.I need to download files.For this I have used the following code.
if(file_exists($folder_path.'/') && is_dir(folder_path)) {
$report_files = glob(folder_path.'/*'.'_*\.pdf' );
if(count($report_files)>0)
{
$result_data = '';
$result_data = rename_filenamespaces($report_files);
var_dump($result_data);//this shows the edited filename
foreach ($result_data as $file) {
if (strpos($file,$_GET['StudentID']) !== false) {
//code for showing the pdf docs to download
}
}
}
}
//function for renaming if filename has spaces
function rename_filenamespaces($location)
{
$new_location = $location;
foreach ($location as $file) {
//check file has spaces and filename has studentID
if((strpos($file," ")!==false)&& (strpos($file,$_GET['StudentID']) !== false))
{
$new_filename = str_replace(" ","-",$file);
rename($file,$new_filename);
$new_location = $new_filename;
}
}
return $new_location;
}
The variable $result_data gives me the filename without spaces,but the for each loop is showing Warning:Invalid argument supplied for foreach(). But the filename is changed in the server directory immediately after running the function. This warning shows only for first time. I am unable to solve this.
$new_location = $new_filename;
$new_location is a array
$new_filename is a string
You have to use $new_location[$index]
or try
foreach ($new_location as &$file) {
...
...
$file = $new_filename;

Upload image file errror after 19 images in php

I am using a php to upload multiple images around 30 or so. I have compressed the image to 100kb before uploading. When I try to upload images via Android then I don't get any error. Only 19 images are transferred to the server but not the rest. I am using cPanel shared hosting account and it's not a website only php.
How can I upload more than 19 images?
/////// images
if (!empty($images_names_str)) {
if( get_magic_quotes_gpc() ) {
$images_names_str = stripslashes( $images_names_str );
}
$newstr = json_decode( $images_names_str,true );
$i = -1;
foreach($newstr as $item) { //foreach element in $arr
$i++;
$name = $item['name']; //etc
$path = $item['path'];
$file_path1 = "/home/public_html/images/";
// Use
if ($_FILES['image'.$i]['error'] === UPLOAD_ERR_OK) {
//uploading successfully done
$moveResult = move_uploaded_file($_FILES['image'.$i]['tmp_name'], $file_path1.$name);
fputs($fp,"File has been moved from to" . $file_path1.$name. "\n");
} else {
fputs($fp,$_FILES['image'.$i]['error'].$i." ERROR: File not moved correctly".$file_path1.$name. "\n");
}
}
echo "done";
}

Delete an image in PHP (WordPress)

I have this code which uploads an image from admin and it works well.
add_action('admin_init', 'register_and_build_fields');
function register_and_build_fields() {
register_setting('theme_options', 'theme_options', 'validate_setting', 'delete_file');
}
function validate_setting($theme_options) {
$keys = array_keys($_FILES); $i = 0; foreach ( $_FILES as $image ) {
// if a files was upload
if ($image['size']) {
// if it is an image
if ( preg_match('/(jpg|jpeg|png|gif)$/', $image['type']) ) { $override = array('test_form' => false);
$options = get_option('theme_options'); echo "<img src='{$options['logo']}' />";
// save the file, and store an array, containing its location in $file
$file = wp_handle_upload( $image, $override ); $theme_options[$keys[$i]] = $file['url']; } else {
// Not an image.
$options = get_option('theme_options'); $theme_options[$keys[$i]] = $options[$logo];
// Die and let the user know that they made a mistake.
wp_die('No image was uploaded or invalid format.<br>Supported formats: jpg, jpeg, png, gif.<br> Go <button onclick="history.back()">Back</button> and try again.'); } } // Else, the user didn't upload a file.
// Retain the image that's already on file.
else { $options = get_option('theme_options'); $theme_options[$keys[$i]] = $options[$keys[$i]]; } $i++; }
return $theme_options;
}
and now I want the function to delete the current image.
function delete_file($theme_options) {
if (array_key_exists('delete_file', $_FILES)) {
$image = $_FILES['delete_file'];
if (file_exists($image)) {
unlink($image);
echo 'File '.$image.' has been deleted';
} else {
echo 'Could not delete '.$image.', file does not exist';
}
}
}
I added the button in admin but is doing.. nothing.
I'm building a TemplateOptions in WordPress and now I'm trying to make the logo function to be uploaded and deleted from admin. Like I said, uploading the logo works and now I want to make the field "delete" to work.
I found out that you don't need to insert the record in the DB as you only have a single image and whose name and path are also constant . So you simply need to unlink the image from the directory and it will be deleted, nothing else.
say your logo.png is in your themes's img folder then you should unlink it like this. default is your theme name
$path = ABSPATH.'wp-content/themes/default/img/logo.png';
if(file_exists($path))
{
unlink( $path );
}
Note: Remember that you have to pass the absolute path to the unlink() and not the url having http , because it will give you an error , you can't use http with unlink.

Upload Entire Directory via PHP to a remote host via FTP

I'm using Codeigniter to create a web site, and I tried to create a function to upload the entire directory via FTP to a remote host, but nothing is working
I tried 2 functions I found, but also not working, only few files uploaded, and some files size is 0 bytes
Functions Used :
// 1St Function
public function ftp_copy($src_dir, $dst_dir) {
$d = dir($src_dir);
while($file = $d->read()) {
if ($file != "." && $file != "..") {
if (is_dir($src_dir."/".$file)) {
if (!#ftp_chdir($this->conn_id, $dst_dir."/".$file)) {
ftp_mkdir($this->conn_id, $dst_dir."/".$file);
}
ftp_copy($src_dir."/".$file, $dst_dir."/".$file);
}
else {
$upload = ftp_put($this->conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY);
}
}
}
$d->close();
}
// 2nd Solution from Stackoverflow question
foreach (glob("/directory/to/upload/*.*") as $filename)
ftp_put($ftp_stream, basename($filename) , $filename, FTP_BINARY);
Any solution ??
If you are using codeigniter you can use $this->ftp->mirror() from their FTP Library.
http://codeigniter.com/user_guide/libraries/ftp.html
$this->load->library('ftp');
$config['hostname'] = 'ftp.example.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['debug'] = TRUE;
$this->ftp->connect($config);
$this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
$this->ftp->close();

Categories