Creating Private File in PHP – File Not Found

Home Forums Main plugin support Creating Private File in PHP – File Not Found

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #262426
    Lance Miller
    Participant

    I’m working on a site that will need hundreds of private files uploaded. To help speed up the process, I’m developing a custom add-on to bulk import the files.

    I’ve got it very close, but I’m hung up on something right now.

    Utilizing a customized version of the cuar_create_private_file function, I can successfully upload a file to the server, create the private file Post, and even attach it to the Post.

    If I click to edit the Post, I can see the file has been attached. But if I try to actually download the file, I get an error that it couldn’t find the file. And if I hit the “Update” button on the Post, the attachment disappears altogether.

    It looks like it’s being added to the server fine (currently it’s making it into the wp-content/customer-area/storage folder. But a new folder with the random numbers is not being created, and the file is not ending up in there. The Post (at least until I make an update) knows about the attachment and knows what folder it should be in, but because that folder doesn’t actually exist, the download fails.

    Does anyone know what function is supposed to create that folder and move the file, or why it’s not working for me?

    Here’s a simplified version of the code I have so far (after the form submission to upload the file(s)):

    function bulk_file_import() {
    				
    	if (isset($_POST) && !empty($_POST)) {
    		
    		if (isset($_FILES)) {
    			
    			$files = $_FILES['fileselect'];												
    			$fileCount = count($files['name']);
    									
    			// loop through files 
    			for ($f = 0; $f < $fileCount; $f++) {
    				
    				// move files into the folder
    				$destFile = "/home/*******/public_html/wp-content/customer-area/storage/" . $files['name'][$f];
    				move_uploaded_file($files['tmp_name'][$f], $destFile);
    											
    				$postdata = array(
    					'post_title' => str_replace(".pdf", "", $files['name'][$f]),
    					'post_content' => "",
    					'post_status' => 'publish',
    					'post_author' => 1
    				);
    				
    				$owners = array(
    				   'usr' => array(1),
    				);
    
    				$files = array(
    					array(
    						'name' => $files['name'][$f],
    						'path' => "/home/*******/public_html/wp-content/customer-area/storage/",
    						'method' => 'copy'
    					)
    				);	
    
    				$insert = custom_cuar_create_private_file($postdata, $owners, $files);
    				
    			}
    		
    		}
    
    	}
    }

    And this is the custom_cuar_create_private_file function:

    function custom_cuar_create_private_file($post_data, $owners, $files) {
    				
    
    	// Create the post object
    	$post_data['post_type'] = 'cuar_private_file';
    	$post_id = wp_insert_post($post_data);
    	if (is_wp_error($post_id)) {
    		return $post_id;
    	}
    	
    	// Assign the owner
    	/** @var CUAR_PostOwnerAddOn $po_addon */
    	$po_addon = cuar_addon('post-owner');								
    	$po_addon->save_post_owners($post_id, $owners);
    								
    									
    	// Attach the file
    	/** @var CUAR_PrivateFileAddOn $pf_addon */
    	$pf_addon = cuar_addon('private-files');
    	foreach ($files as $file) {
    		$initial_filename = basename($file['name']);
    		$filename = apply_filters('cuar/private-content/files/unique-filename?method=local',
    			$initial_filename,
    			$post_id,
    			$file);
    			
    		$caption = $filename;
    		$extra = '';
    
    		$errors = apply_filters('cuar/private-content/files/on-attach-file?method=local',
    			array(),
    			$pf_addon,
    			$initial_filename,
    			$post_id,
    			$filename,
    			$caption,
    			$extra);
    
    		// update file meta 
    		$source = apply_filters('cuar/private-content/files/file-source?method=local', 'local');
    		if (empty($caption)) $caption = $filename;
    		
    		$post_id = (string)$post_id;
    				
    		$added_file = $pf_addon->add_attached_file($post_id, $filename, $caption, $source, $extra);
    				
    		// log event 
    		do_action('cuar/private-content/files/on-add-attachment', $post_id, $added_file);
    		
    		if ( !empty($errors)) {
    			wp_delete_post($post_id);
    
    			return new WP_Error('upload_error', implode(', ', $errors));
    		}
    	}
    
    	return $post_id;
    }
    #262508
    Thomas Lartaud
    Keymaster

    Hi,

    Does anyone know what function is supposed to create that folder and move the file, or why it’s not working for me?

    Well, it looks like you are moving the file yourself. Did you check that the file gets found and correctly moved around this function?

    // move files into the folder
    $destFile = "/home/*******/public_html/wp-content/customer-area/storage/" . $files['name'][$f];
    move_uploaded_file($files['tmp_name'][$f], $destFile);
    

    If not, it should probably mean that the move_uploaded_file is not doing the job?

    #262509
    Lance Miller
    Participant

    Thanks for your response, Thomas!

    Yes, it is successfully getting into that folder.

    So if I go to http://www.mywebsite.com/wp-content/customer-area/storage/my-file.pdf, it comes up.

    The problem is that the Post itself is looking for something more like this:

    http://www.mywebsite.com/wp-content/customer-area/storage/123abc456def789ghi0/my-file.pdf

    So it’s failing to permanently keep the attachment because the file isn’t actually in that folder.

    Do you know at what point in the normal Customer Area upload process does that random-string folder get created?

    I thought maybe the random string was actually the ID for the file, but based off of my own tests, that doesn’t seem to actually be the case.

    #262562
    Thomas Lartaud
    Keymaster

    Hi,

    Do you know at what point in the normal Customer Area upload process does that random-string folder get created?

    Hmm, I guess this is processed in $pf_addon->add_attached_file (wp-plugins/customer-area/src/php/core-addons/private-file/private-file-addon.class.php L367).

    Line 378 you’ll see
    $meta['id'] = $this->compute_file_id($meta);

    This function looks like this

    
    /**
             * Compute an ID from the file name
             *
             * @param string $filename The file name
             *
             * @return string The ID
             */
            public function compute_file_id($file)
            {
                $id = md5($file['file']);
    
                return apply_filters('cuar/private-content/files/file-id', $id, $file);
            }
    

    So you can either use the filter to give your own ID or apply the same md5 on the filename.

    Maybe not the best answer. I’ll ask @vprat to take a look at this thread to see if he has something better for you.

    Regards.

    #525482
    ondraATH
    Participant

    Hi, Lance Miller, I read this whole thread, copied the functions (both functions and pasted them into functions-private-files.php function), edited $destFile and now I don’t know how my own works.

    I want to ask if this feature is function and how could function work work?

    Thank you,
    Ondrej

    #525682
    Thomas Lartaud
    Keymaster

    Hi @ondraath

    I removed the set this post as private reply from your reply because only Vincent and I can see those.

    both functions and pasted them into functions-private-files.php function

    Never edit plugin files, or you’ll lose your edits after updating the plugin.

    I suppose that @mediaascentdigitalmedia-com created a custom WP admin page where he can execute this script properly.

    You’ll find many tutorials on the web to do that. This is a standard WP coding feature.

    However, if you don’t understand what is being done on this script, I would recommend you to hire a developer from your region to do it for you. It would require just a few hours for any decent WP developer with this example to adapt it to your needs.

    Best regards.

    #526967
    ondraATH
    Participant

    Thanks for links. I’m a beginner with programming and WordPress, but I believe that I can do it with your help and google.

    #527428
    Thomas Lartaud
    Keymaster

    Hi,

    If I understand correctly from your other post, you’re trying to import a CSV and then perform some bulk actions.
    This post is not exactly intended to do that, however, it can help a developer (intermediate or advanced level) to achieve what you want to do.

    The problem is that what you want to do is very specific to your needs. We don’t do such customizations. This is outside of the scope of the support. I’m sure you can understand that we can’t spend our days making additionals scripts for our customers, making their menus looks as they wish, or anything else that could be imagined. The goal of this support is to lead you to the right way if you need some assistance or to debug your actual issues to make the plugin work as expected.

    In your case, you’re saying that you are a beginner with programming and WordPress, so I would highly recommend you to hire a developer from your region to do it for you.
    There is everything in this post that a developer would require to achieve what you need. We can’t do really more actually, and we are too busy on the incoming releases to do something else.

    I hope you’ll understand.

    Regards.

    #634045
    Daniel M
    Participant

    Hello! I have read all the topics and threads to get the function of massively importing files and associating them with each user. Is there anything new for this? My customer area works for more than 3000 employees, I would appreciate some light to get it. Many thanks!

    #634052
    Daniel M
    Participant

    through wp import pro and its custom fields I get to see in the admin panel, files, as if it is associated with each user however in the user panel it is added as unknown.

    On the other hand, in the import, it shows a file to download called “h” my file has another name, I get a little lost in that performance.

    #634210
    Thomas Lartaud
    Keymaster

    Hi Daniel.

    You have a full code example here. We can help you debug our functions, but we can’t help everybody to learn PHP and parse a filename (because all of you needs different ways to convert their filenames to owners assignments, so the code might differ for each of you. There is no solution that could work for everybody for this).
    If you can’t understand what this script is doing, I recommend you to hire a developer, and ask him to adapt this code to your needs.

    Regards.

    #643059
    carlospart
    Member

    Hello! I have read all the topics and threads to get the function of massively importing files and associating them with each user. Is there anything new for this? My customer area works for more than 3000 employees, I would appreciate some light to get it. Many thanks!

    Hello! Have you found a solution for this? I’m in a similar situation.

    #651043
    Daniel M
    Participant

    Hola! si tengo solución

    1 user thanked author for this post.
    #655399
    ganjah
    Member

    Could you tell us how you solved the problem?

    I’m in a similar situation but I can’t make any headway.

    Thank you

    Hola! si tengo solución

    Could you tell us how you solved the problem?

    I’m in a similar situation but I can’t make any headway.

    Thank you

    #655517
    Daniel M
    Participant

    Yo lo e conseguido desarrollando un plugin desde 0 que trabaja con wp customer área.

Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.