Skip to main content
Inspiring
November 2, 2010
Answered

Uploading files using Zend Framework

  • November 2, 2010
  • 2 replies
  • 5814 views

I am tyring to use the Zend Framework to upload files as outlined in the "Adobe Dreamweaver CS5 with PHP" book by David Powers.  I've been using the example code (more or less), and can get it to work perfectly with a local XAMPP installation of Apache and PHP.  Howver, I can't get it to work successfully with a hosted site, having tried both GoDaddy and Verio.

The Zend Framework is correctly installed, include paths set correctly (I know this because other Zend FW operations, such as validation work fine).  In addition, if the directory I want to upload to isn't correctly specified I get errors on that.  But when I try to actually upload a file (again, with code that works on the XAMPP platform), I get nothing.  No errors, and no uploaded file.

I have the script set up to send an email with a link to the uploaded file, and the name field is blank (but the email does get sent, again indicating that the ZF is working).  I'm concluding I have something incorrectly configured on the hosting plaforms, and am looking for suggestions as what to look at.  Thanks.

BTW, checked the permissions on the upload directory, and it should be writeable...

This topic has been closed for replies.
Correct answer David_Powers

Ugh, I'm profoundly embarrassed - I had created two upload directories - one off my site root and one off a subfolder, and was looking in the wrong place for the uploaded files.  Duh... now that I'm looking in the right place, sure enough there they are.

Sorry for wasting your time, and thanks for your help.

Great book BTW.  Everything works as long as the coder pays attention to what they're doing... :-(


No problems. It's a mistake we've all made at one time or another. Glad you find the book useful.

2 replies

antares5Author
Inspiring
November 5, 2010

David,

Thanks for the reply.  I neglected to mention that file upload do work on the server, it's just the Zend FW implementation that doesn't.  The Zend FW is basically what is in your book, and nothing happens when I try an upload, but I know that the code is talking to the FW (e.g., if I provide an invalid path, I get the appropraite PHP error).  If I don't use the FW it works.  So the following code correctly uploads a file on the hosting platform:

if ($_POST['send']) {
//validate the user input
$val = new Zend_Validate_Alnum(TRUE);
if (!$val->isValid($_POST['name'])) {
   $errors['name'] = 'Name is required';
}
$val = new Zend_Validate_EmailAddress();
if (!$val->isValid($_POST['email'])) {
   $errors['email'] = 'Email address is required';
}
$val = new Zend_Validate_StringLength(10);
if (!$val->isValid($_POST['message'])) {
   $errors['message'] = 'Required';
}

if (!$errors) {
  foreach($_FILES as $temp_name => $file_array) {
   $file_name = str_replace(" ","_",$file_array["name"]);
 
   if (is_uploaded_file($file_array["tmp_name"])) {
    move_uploaded_file($file_array["tmp_name"], "$file_dir/".$file_name) or die ("Couldn't copy");
   }
   $new_names[] = $file_name;
  }
  //start building the mail string
  $msg = "Name:\n\t\t".$_POST["name"]."\n";
  $msg .= "E-Mail:\n\t\t".$_POST["email"]."\n";
  $msg .= "Message:\n\t\t".$_POST["message"]."\n";
 
  foreach ($new_names as $name) {
   if ($name != "") {
   $msg .=  "Link: xxx/uploads/$name"."\n\n";

Using this code I wil email that contains links to the uploaded files, which I can then sucessfully download.  So it's the FW code that fails... and note that the Zend validation code above does work correctly; I get errors if invalid entries are made.

Also, I note that my hosting providers only support PHP 5.2.14, whereas on my XAMPP installation I'm using 5.3  Could that be an issue?

David_Powers
Inspiring
November 6, 2010

antares5 wrote:

Also, I note that my hosting providers only support PHP 5.2.14, whereas on my XAMPP installation I'm using 5.3  Could that be an issue?

No. Zend Framework 1.x works with PHP 5.2.

Try the following:

$destination = 'C:/upload_test';  // set to correct location
$uploader = new Zend_File_Transfer_Adapter_Http();
$uploader->setDestination($destination);

if (!$uploader->receive()) {

  $messages = $uploader->getMessages();

  print_r($messages);

}

David_Powers
David_PowersCorrect answer
Inspiring
November 8, 2010

Ugh, I'm profoundly embarrassed - I had created two upload directories - one off my site root and one off a subfolder, and was looking in the wrong place for the uploaded files.  Duh... now that I'm looking in the right place, sure enough there they are.

Sorry for wasting your time, and thanks for your help.

Great book BTW.  Everything works as long as the coder pays attention to what they're doing... :-(


No problems. It's a mistake we've all made at one time or another. Glad you find the book useful.

David_Powers
Inspiring
November 5, 2010

Have you checked the configuration on the remote server? Is file_uploads On?