Jump to content

Recommended Posts

Ok i have a class that uploads files, but i also would like it to submit without files being uploaded, the file only submits if a file has been uploaded, i want users to have the option not to upload if they didn't need to here is my code class

 

class Photograph{

public $id;
public $filename;
public $type;
public $size;
public $content;
public $caption;
public $category_id;
public $admin_id;
public $created;
private $temp_path;
protected $upload_dir = "images";
public $errors = array();
protected $upload_errors = array(
UPLOAD_ERR_OK => "NO errors",
UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize",
UPLOAD_ERR_FROM_SIZE => "Larger than form MAX_FILE_SIZE",
UPLOAD_ERR_PARTIAL => "Partal upload",
UPLOAD_ERR_NO_FILE => "No file",
UPLOAD_ERR_NO_TMP_DIR => "No temporary directory",
UPLOAD_ERR_CANT_WRITE => "Can't write to disk",
UPLOAD_ERR_EXTENSION => "File upload stopped by extension",
);
//passes in $_FILE(['uploaded_file']) as an argument!
 public function attach_file($file="") {
 // Perform error checking on the form parameters
 if(!$file || empty($file) || !is_array($file)) {
   // error: nothing uploaded or wrong argument usage
   $this->errors[] = "No file was uploaded.";
   return false;
 } elseif($file['error'] != 0) {
   // error: report what PHP says went wrong
   $this->errors[] = $this->upload_errors[$file['error']];
   return false;
 } else {
  // Set object attributes to the form parameters.
   $this->temp_path  = $file['tmp_name'];
   $this->filename   = basename($file['name']);
   $this->type	   = $file['type'];
   $this->size	   = $file['size'];
  // Don't worry about saving anything to the database yet.
  return true;
 }
}


public function getcontent(){
strlen($this->content) > 10;
}

 public function save() {
 // A new record won't have an id yet.
 if(isset($this->id)) {
  // Really just to update the caption
  $this->update();
 } else {
  // Make sure there are no errors

  // Can't save if there are pre-existing errors
   if(!empty($this->errors)) { return false; }

  // Make sure the caption is not too long for the DB
   if(strlen($this->caption) > 255) {
   $this->errors[] = "The caption can only be 255 characters long.";
   return false;
  }

   if(empty($this->caption)) {
   $this->errors[] = "please add a caption.";
   return false;
  }

   // Can't save without filename and temp location
   if(empty($this->filename) || empty($this->temp_path)) {
  $this->errors[] = "The file location was not available.";
  return false;
   }

  // Determine the target_path
   $target_path = "/home/designanddev.co.uk/public_html/blog/".$this->upload_dir."/".$this->filename;

   // Make sure a file doesn't already exist in the target location
   if(file_exists($target_path)) {
  $this->errors[] = "The file {$this->filename} already exists.";
  return false;
   }

  // Attempt to move the file
  if(move_uploaded_file($this->temp_path, $target_path)) {
 // Success
   // Save a corresponding entry to the database
   if($this->create()) {
 // We are done with temp_path, the file isn't there anymore
 unset($this->temp_path);
 return true;
   }
  } else {
   // File was not moved.
  $this->errors[] = "The file upload failed, possibly due to incorrect permissions on the upload folder.";
  return false;
  }
 }
}

Link to comment
https://forums.phpfreaks.com/topic/273735-image-upload-help/
Share on other sites

You know, this is quite basic stuff. So it would seem that you might want to spend a little bit more time studying the basics of PHP, as well as planning (with pen an paper) how you want your code to work. Doing so will make things a lot cleared for you, and save you a lot of time in the long run.

 

As for the example:

// Only create uploader object if a file has been uploaded.
if (!empty ($_FILES['photo']['name'])) {
   $uploader = new Photograph ();

   // Code to handle the upload.
}

Link to comment
https://forums.phpfreaks.com/topic/273735-image-upload-help/#findComment-1408774
Share on other sites

You know, this is quite basic stuff. So it would seem that you might want to spend a little bit more time studying the basics of PHP, as well as planning (with pen an paper) how you want your code to work. Doing so will make things a lot cleared for you, and save you a lot of time in the long run.

 

As for the example:

// Only create uploader object if a file has been uploaded.
if (!empty ($_FILES['photo']['name'])) {
$uploader = new Photograph ();

// Code to handle the upload.
}

 

Thanks a lot, will work on it

Link to comment
https://forums.phpfreaks.com/topic/273735-image-upload-help/#findComment-1408787
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.