We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 50920
    • 6 Posts
    Quote from: kudykam at Mar 15, 2013, 06:39 AM
    Got problem with the latest snippet code, it sucessfully upload image but input this in the TV:
    (filename).jpg||image/jpeg||/tmp/php5/phpFi8L01||0||657369


    instead of full path.
    How can I change filename to resource id for which is file uploaded? ([*id]-some_extension.jpg)

    Hello.
    I have same problem. How to solve this error? Please.
      • 42830
      • 5 Posts
      Quote from: antonfix at Jan 06, 2017, 08:34 PM
      Quote from: kudykam at Mar 15, 2013, 06:39 AM
      Got problem with the latest snippet code, it sucessfully upload image but input this in the TV:
      (filename).jpg||image/jpeg||/tmp/php5/phpFi8L01||0||657369


      instead of full path.
      How can I change filename to resource id for which is file uploaded? ([*id]-some_extension.jpg)

      Hello.
      I have same problem. How to solve this error? Please.

      I remember that i had run into this problem too, but i can't remember what i changed in the code to solve it.
      This one worked for me. I used it for PDF files.

      <?php
      // initialize output;
      $output = true;
       
      // valid extensions
      $ext_array = array('pdf', 'txt', 'doc', 'docx', 'rtf');
       
      // create unique path for this form submission
      $uploadpath = 'uploads/';
      
      // you can create some logic to automatically
      // generate some type of folder structure here.
      // the path that you specify will automatically
      // be created by the script if it doesn't already
      // exist.
      
      // EXAMPLE:
      // this would put all file uploads into a new,
      // unique folder every day.
      // $uploadpath = 'assets/'uploads/'.date('Y-m-d').'/';
       
      // get full path to unique folder
      $target_path = $modx->config['base_path'] . $uploadpath;
       
      // get uploaded file names:
      $submittedfiles = array_keys($_FILES);
       
      // loop through files
      foreach ($submittedfiles as $sf) {
       
          // Get Filename and make sure its good.
          $filename = basename( $_FILES[$sf]['name'] );
       
          // Get file's extension
          $ext = pathinfo($filename, PATHINFO_EXTENSION);
          $ext = mb_strtolower($ext); // case insensitive
       
          // is the file name empty (no file uploaded)
          if($filename != '') {
               
              // is this the right type of file?
              if(in_array($ext, $ext_array)) {
      
                  // clean up file name and make unique
                  $filename = mb_strtolower($filename); // to lowercase
                  $filename = str_replace(' ', '_', $filename); // spaces to underscores
                  $filename = date("Y-m-d_G-i-s_") . $filename; // add date & time
                   
                  // full path to new file
                  $myTarget = $uploadpath . $filename;
                   
                  // create directory to move file into if it doesn't exist
                  mkdir($target_path, 0755, true);
                   
                  // is the file moved to the proper folder successfully?
                  if(move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) {
                      // set a new placeholder with the new full path (if you need it in subsequent hooks)
                      //$modx->setPlaceholder('fi.'.$sf.'_new', $myTarget);
      
      				//$modx->setPlaceholder('target', $myTarget);
      				$hook->setValue('PDFfile',$myTarget);
      
      				// set the permissions on the file
                      if (!chmod($myTarget, 0644)) { /*some debug function*/ }
                       
                  } else {
                      // File not uploaded
                      $errorMsg = 'There was a problem uploading the file.';
                      $hook->addError($sf, $errorMsg);
                      $output = false; // generate submission error
                  }
               
              } else {
                  // File type not allowed
                  $errorMsg = 'Type of file not allowed.';
                  $hook->addError($sf, $errorMsg);
                  $output = false; // generate submission error
              }
           
          // if no file, don't error, but return blank
          } else {
              $hook->setValue($sf, '');
          }
      }
       
      return $output;

        • 50920
        • 6 Posts
        Quote from: desper at Jan 11, 2017, 08:09 PM
        Quote from: antonfix at Jan 06, 2017, 08:34 PM
        Quote from: kudykam at Mar 15, 2013, 06:39 AM
        Got problem with the latest snippet code, it sucessfully upload image but input this in the TV:
        (filename).jpg||image/jpeg||/tmp/php5/phpFi8L01||0||657369


        instead of full path.
        How can I change filename to resource id for which is file uploaded? ([*id]-some_extension.jpg)

        Hello.
        I have same problem. How to solve this error? Please.

        I remember that i had run into this problem too, but i can't remember what i changed in the code to solve it.
        This one worked for me. I used it for PDF files.

        <!--?php
        // initialize output;
        $output = true;
         
        // valid extensions
        $ext_array = array('pdf', 'txt', 'doc', 'docx', 'rtf');
         
        // create unique path for this form submission
        $uploadpath = 'uploads/';
        
        // you can create some logic to automatically
        // generate some type of folder structure here.
        // the path that you specify will automatically
        // be created by the script if it doesn't already
        // exist.
        
        // EXAMPLE:
        // this would put all file uploads into a new,
        // unique folder every day.
        // $uploadpath = 'assets/'uploads/'.date('Y-m-d').'/';
         
        // get full path to unique folder
        $target_path = $modx--->config['base_path'] . $uploadpath;
         
        // get uploaded file names:
        $submittedfiles = array_keys($_FILES);
         
        // loop through files
        foreach ($submittedfiles as $sf) {
         
            // Get Filename and make sure its good.
            $filename = basename( $_FILES[$sf]['name'] );
         
            // Get file's extension
            $ext = pathinfo($filename, PATHINFO_EXTENSION);
            $ext = mb_strtolower($ext); // case insensitive
         
            // is the file name empty (no file uploaded)
            if($filename != '') {
                 
                // is this the right type of file?
                if(in_array($ext, $ext_array)) {
        
                    // clean up file name and make unique
                    $filename = mb_strtolower($filename); // to lowercase
                    $filename = str_replace(' ', '_', $filename); // spaces to underscores
                    $filename = date("Y-m-d_G-i-s_") . $filename; // add date & time
                     
                    // full path to new file
                    $myTarget = $uploadpath . $filename;
                     
                    // create directory to move file into if it doesn't exist
                    mkdir($target_path, 0755, true);
                     
                    // is the file moved to the proper folder successfully?
                    if(move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) {
                        // set a new placeholder with the new full path (if you need it in subsequent hooks)
                        //$modx->setPlaceholder('fi.'.$sf.'_new', $myTarget);
        
        				//$modx->setPlaceholder('target', $myTarget);
        				$hook->setValue('PDFfile',$myTarget);
        
        				// set the permissions on the file
                        if (!chmod($myTarget, 0644)) { /*some debug function*/ }
                         
                    } else {
                        // File not uploaded
                        $errorMsg = 'There was a problem uploading the file.';
                        $hook->addError($sf, $errorMsg);
                        $output = false; // generate submission error
                    }
                 
                } else {
                    // File type not allowed
                    $errorMsg = 'Type of file not allowed.';
                    $hook->addError($sf, $errorMsg);
                    $output = false; // generate submission error
                }
             
            // if no file, don't error, but return blank
            } else {
                $hook->setValue($sf, '');
            }
        }
         
        return $output;


        Thank you!
          • 53127
          • 15 Posts
          Quote from: debussy at Jan 21, 2011, 10:21 AM
          I modified the hook/snippet so that it can accept multiple files and can auto-create the upload directory if it doesn't exist.

          *Note: that if the user uploads multiple files with the same name they will overwrite each other unless you add some type of random salt to the file name (more than just a date/time stamp).
          *Note: also, don't forget to add the requisite enctype="multipart/form-data" to your <form> tag! ... I spent an embarrassing amount of time trying to figure out what was wrong before having a "doh!" moment!

          <!--?php
          
          // initialize output;
          $output = true;
          
          // valid extensions
          $ext_array = array('pdf', 'txt', 'doc', 'docx', 'rtf');
          
          // create unique path for this form submission
          $uploadpath = 'assets/uploads/';
          
          // you can create some logic to automatically
          // generate some type of folder structure here.
          // the path that you specify will automatically
          // be created by the script if it doesn't already
          // exist.
          
          // EXAMPLE:
          // this would put all file uploads into a new,
          // unique folder every day.
          // $uploadpath = 'assets/'uploads/'.date('Y-m-d').'/';
          
          // get full path to unique folder
          $target_path = $modx--->config['base_path'] . $uploadpath;
          
          // get uploaded file names:
          $submittedfiles = array_keys($_FILES);
          
          // loop through files
          foreach ($submittedfiles as $sf) {
          
          	// Get Filename and make sure its good.
          	$filename = basename( $_FILES[$sf]['name'] );
          
          	// Get file's extension
          	$ext = pathinfo($filename, PATHINFO_EXTENSION);
          	$ext = mb_strtolower($ext); // case insensitive
          
          	// is the file name empty (no file uploaded)
          	if($filename != '') {
              	
          		// is this the right type of file?
          		if(in_array($ext, $ext_array)) {
           	
          			// clean up file name and make unique
          			$filename = mb_strtolower($filename); // to lowercase
          			$filename = str_replace(' ', '_', $filename); // spaces to underscores
          			$filename = date("Y-m-d_G-i-s_") . $filename; // add date & time
          			
          			// full path to new file
          			$myTarget = $target_path . $filename;
          			
          			// create directory to move file into if it doesn't exist
          			mkdir($target_path, 0755, true);
          			
          			// is the file moved to the proper folder successfully?
          			if(move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) {
          				// set a new placeholder with the new full path (if you need it in subsequent hooks)
          				$modx->setPlaceholder('fi.'.$sf.'_new', $myTarget);
          				// set the permissions on the file
          				if (!chmod($myTarget, 0644)) { /*some debug function*/ }
          				
                  	} else {
          				// File not uploaded
          				$errorMsg = 'There was a problem uploading the file.';
          				$hook->addError($sf, $errorMsg);
          				$output = false; // generate submission error
          			}
              	
              	} else {
          			// File type not allowed
              	    $errorMsg = 'Type of file not allowed.';
          			$hook->addError($sf, $errorMsg);
          			$output = false; // generate submission error
                  }
          	
          	// if no file, don't error, but return blank
          	} else {
          	    $hook->setValue($sf, '');
          	}
          
          }
          
          return $output;
          
          ?>
          

          I don't use formit2file often and sadly both times I have tried implementing this, the enctype='multipart/form-data' got me for hours the first time and days this time! GRRRR lol. IT REALLY HELPS TO READ ALL OF THE POSTS AND NOTES EVEN WHEN YOU DON'T THINK IT'S NECESSARY. How many times do I have to forget that. Thanks everyone!!