Zend_Form_Element_File multiple file upload and mysql - zend-framework

Does anybody knows a working example to receive multiple files, store them in a folder and the names in a mysql table? Some days already with no luck at all, something always missing, and before putting those questions, well , maybe i can find the right one!
maybe?
i am learning using zend 1.11
thanks
pablo
EDIT:
i add the code for clearness:
in the form:
$element = new Zend_Form_Element_File('images');
$element->setLabel('Upload bis 3 Bilder (máx. 200kb each):')
->setMultiFile(3)
->setValueDisabled(true)
->addValidator(new Zend_Validate_File_Size('2MB'))
->addValidator('Count', false, array('min'=>0,'max' => 3));
in the controller:
$adapter = $form->images->getTransferAdapter();
//create directory where files would be hold
if (!file_exists(UPLOADDIR))
mkdir(UPLOADDIR, 0777, 1);
$i=0;
$images="";
//loop uploaded files
foreach ($adapter->getFileInfo() as $info)
{
//rename file how you like and move it to given destination
$fileName = time().$i.'.'.$this->getExtension($info['name']);
$adapter->addFilter('Rename', array('target'=>UPLOADDIR.$fileName, 'overwrite'=>true));
//if something goes wrong print errors in screen
if (!$adapter->receive($info['name']))
{
die(print_r($adapter->getMessages(),1));
}else{
if ($info['name']!==""){
$images .= $fileName.",";
}
}
$i++;
}

I think you should use setFilters() instead of addFilter on the adapter, because the current code will add a new Rename filter on each iteration of the foreach loop.
Try replacing:
$adapter->addFilter('Rename', array('target'=>UPLOADDIR.$fileName, 'overwrite'=>true));
with
$adapter->setFilters(array(new Zend_Filter_File_Rename(array('target'=>UPLOADDIR.$fileName, 'overwrite'=>true))));

Related

How to display uploaded csv file into tablular format in drupal 7

I have created a form in drupal 7 and it has a field to upload file (csv file only ) now how to display uploaded csv file into table on form submit ?
Not sure about theme function, but you can do it on your own.
I.e. use:
http://php.net/manual/en/function.file-get-contents.php
To read the file and then:
http://php.net/manual/en/function.str-getcsv.php
to parse CSV.
Or, maybe:
http://php.net/manual/en/function.fgetcsv.php
to read row by row. Anyway, you'll end up with looping trough rows so just print values the way you want, add markup around the values...
After go through various code exercises i have come up with solution which is as follows :
`function display_table($filename, $head=false) {
$handle = fopen($filename, "r");
$all_rows = array();
$header = null;
while ($row = fgetcsv($handle)) {
if ($header === null) {
$header = $row;
continue;
}
$all_rows[] = array_combine($header, $row);
}
$table = theme('table', array('header' => $header, 'rows' => $all_rows));
return $table;
}`
Hope it would be helpful to others as well !

Zend File multiple upload with access to seperate file names

I use Zend Framework 1.12 for some file upload system. And using Zend_File_Transfer_Adapter_Http in a form, for upload of two files.
There are two form elements for those two files.
$file1 = new Zend_Form_Element_File('file1');
// other options like setLabel etc.
$this->addElement($file1, 'file1');
$file2 = new Zend_Form_Element_File('file2');
// other options like setLabel etc.
$this->addElement($file2, 'file2');
and I handle the upload process in my controller like this:
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->setDestination($dirname);
$files = $adapter->getFileInfo();
foreach ($files as $file => $fileInfo) {
if (!$adapter->receive($file)) {
$messages = $adapter->getMessages();
echo implode("\n", $messages);
} else {
$location = $adapter->getFileName($file, true);
$filename = $adapter->getFileName($file, false);
// taking location and file names to save in database..
}
}
}
With these, I can manage upload of two files. But I don't know how to take location of files which is uploaded with the specific Zend_Form_Element_File. For example, i need to know which file is uploaded to $file1 (element in form) and I will save its location to a table in database, and which file is uploaded to $file2 and save its location to another table.
I do not like to use Zend_File_Transfer_Adapter_Http.
I prefer to use code like this:
in application.ini:
data_tmp = APPLICATION_PATH "/../data/tmp"
in Bootstrap:
$options = $this->getOptions();
define('DATA_TMP', $options['data_tmp']);
in form:
$elementFoo = new Zend_Form_Element_File('foo');
$elementFoo->setLabel('Upload File 1:')->setDestination(DATA_TMP);
$elementBar = new Zend_Form_Element_File('bar');
$elementBar->setLabel('Upload File 2:')->setDestination(DATA_TMP);
in controller:
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
$values = $form->getValues();
$filenameFoo = $values['foo'];
$filenameBar = $values['bar'];
//at this point you know the name of the individual filename
$filePathFoo = DATA_TMP . DIRECTORY_SEPARATOR . $filenameFoo;
$filePathBar = DATA_TMP . DIRECTORY_SEPARATOR . $filenameBar;
//now you have even the physical path of the files
// taking location and file names to save in database..
}
}
my 2 cent.

Perl mechanize Find all links array loop issue

I am currently attempting to create a Perl webspider using WWW::Mechanize.
What I am trying to do is create a webspider that will crawl the whole site of the URL (entered by the user) and extract all of the links from every page on the site.
But I have a problem with how to spider the whole site to get every link, without duplicates
What I have done so far (the part im having trouble with anyway):
foreach (#nonduplicates) { #array contain urls like www.tree.com/contact-us, www.tree.com/varieties....
$mech->get($_);
my #list = $mech->find_all_links(url_abs_regex => qr/^\Q$urlToSpider\E/); #find all links on this page that starts with http://www.tree.com
#NOW THIS IS WHAT I WANT IT TO DO AFTER THE ABOVE (IN PSEUDOCODE), BUT CANT GET WORKING
#foreach (#list) {
#if $_ is already in #nonduplicates
#then do nothing because that link has already been found
#} else {
#append the link to the end of #nonduplicates so that if it has not been crawled for links already, it will be
How would I be able to do the above?
I am doing this to try and spider the whole site to get a comprehensive list of every URL on the site, without duplicates.
If you think this is not the best/easiest method of achieving the same result I'm open to ideas.
Your help is much appreciated, thanks.
Create a hash to track which links you've seen before and put any unseen ones onto #nonduplicates for processing:
$| = 1;
my $scanned = 0;
my #nonduplicates = ( $urlToSpider ); # Add the first link to the queue.
my %link_tracker = map { $_ => 1 } #nonduplicates; # Keep track of what links we've found already.
while (my $queued_link = pop #nonduplicates) {
$mech->get($queued_link);
my #list = $mech->find_all_links(url_abs_regex => qr/^\Q$urlToSpider\E/);
for my $new_link (#list) {
# Add the link to the queue unless we already encountered it.
# Increment so we don't add it again.
push #nonduplicates, $new_link->url_abs() unless $link_tracker{$new_link->url_abs()}++;
}
printf "\rPages scanned: [%d] Unique Links: [%s] Queued: [%s]", ++$scanned, scalar keys %link_tracker, scalar #nonduplicates;
}
use Data::Dumper;
print Dumper(\%link_tracker);
use List::MoreUtils qw/uniq/;
...
my #list = $mech->find_all_links(...);
my #unique_urls = uniq( map { $_->url } #list );
Now #unique_urls contains the unique urls from #list.

addFilter Rename ZendFramework

i need to Rename a file with Zend_File_Transfer() only if new file match with old one in the server using some convention like newfile-1.ext where the -1 is the string that is added but Rename filter is strange, i really dont understand so good.
For example, is necesary some like this:
if(file_exists($file)){
$upload->addFilter('Rename', $file);
}
or Rename does it?
thanks
Here is an example from one of my apps. File is recvieved by an Zend_Form
$upload->receive();
$name = $upload->getFileName();
$newFile = 'mynewfile.xyz'
$filterFileRename = new Zend_Filter_File_Rename(array(
'target' => $this->path . $newFile, // path to file
'overwrite' => true
));
$file = $filterFileRename->filter($name);

File Upload In Zend

I have a Zend form with some elements like this :
http://i27.tinypic.com/ogj88i.jpg
I added all element using this way:
$element = $this->CreateElement('text','lockerComb');
$element->setLabel('Locker');
$element->setAttrib('class','colorbox');
$elements[] = $element;
$element = $this->CreateElement('text','parking');
$element->setLabel('Automobile / Parking');
$element->setAttrib('class','colorbox');
$elements[] = $element;
$element = $this->CreateElement('text','customes');
$element->setLabel('Customes Fields');
$element->setAttrib('class','colorbox');
$elements[] = $element;
But when i try to create element for file upload i fail..
Can you give more information about the exact nature of your fail?
It's fairly straight forwards to use. From the docs:
$element = new Zend_Form_Element_File('foo');
$element->setLabel('Upload an image:')
->setDestination('/var/www/upload');
Which is basic usage.
It's easy to get the file path wrong, but you should get an error if the path is wrong.
Supplying the code you are using would help!
I have written tutorial handling multiple file uploads with Zend Framework maybe it would help for you. Here is link to tutorial http://irmantasplius.blogspot.com/2009/08/zendform-multiple-file-uploads.html