• Resolved Daniel P.

    (@danidub)


    Hello I’m trying to create a new custom post using a frontend custom form with file upload.

    I have setup a field under the pod fields. Following the example on pods docs

    $pod = pods( 'candidato' );
    
    $url_of_image = 'https://placekitten.com/400/600';
    
    $attachment_id = pods_attachment_import( $url_of_image );
    
    if ( $attachment_id ) {
        $data = array(
            'post_title' => 'My item',
            'my_image'   => $attachment_id
        );
        $pod->add( $data );
    } else {
        echo 'Image not imported, try again.';
    }

    This returns Image not imported, try again because the return from pods_attachment_import returns 0. The image is uploaded under uploads/2021/12 without extension. Also, the image is not listed on the media library and no sizes are created, I have only a file named 600 (the end of the URL https://placekitten.com/400/600).

    I don’t know what I’m doing wrong.
    Any help will be appreciated. Thank you.

    • This topic was modified 2 years, 10 months ago by Daniel P..
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Paul Clark

    (@pdclark)

    $pod = pods( 'candidato' );
    
    $url_of_image = 'https://placekitten.com/400/600?&.jpg';
    
    $title = 'My item';
    
    $attachment_id = media_sideload_image( $url_of_image, null, $title, 'id' );
    
    if ( $attachment_id ) {
    	$data = array(
    		'post_title' => $title,
    		'my_image'   => $attachment_id
    	);
    	$pod->add( $data );
    } else {
    	echo 'Image not imported, try again.';
    }
    Thread Starter Daniel P.

    (@danidub)

    Hello @pdclark. Thank you for your help.

    Any ideas on how to integrate this code with a file upload within a form?

    I have tested something like:

    
    <form method="post" enctype="multipart/form-data">
        <input type="file" id="curriculo" name="curriculo">
        <input type="submit" name="enviar">
    </form>
    
    
    $pod = pods( 'candidato' );
    
    $url_of_image = $_FILES["curriculo"]["tmp_name"];
    
    $title = 'My item';
    
    $attachment_id = pods_attachment_import( $url_of_image );
    
    if ( $attachment_id ) {
    	$data = array(
    		'post_title' => $title,
    		'my_image'   => $attachment_id
    	);
    	$pod->add( $data );
    } else {
    	echo 'Image not imported, try again.';
    }

    But I get Image not imported, try again. (0) in the return regarding again to file extension. I think this happens because the file uploaded under $_FILES["curriculo"]["tmp_name"] is without extension.

    Any ideas oh how to upload and link an image from a custom form?
    Thank you.

    • This reply was modified 2 years, 10 months ago by Daniel P..
    • This reply was modified 2 years, 10 months ago by Daniel P..
    Plugin Support Paul Clark

    (@pdclark)

    I would either validate the form to make sure file has an extension, or check for extension in PHP using pathinfo(), and if not set, use rename() to force an extension.

    Plugin Support Paul Clark

    (@pdclark)

    Also, have you tried the [pods-form] shortcode or block?

    e.g.,
    [pods-form name="candidato" fields="my_image"]

    Thread Starter Daniel P.

    (@danidub)

    Hello @pdclark,

    I need to use a custom form because of some payment functionalities and cannot use the [pods-form] shortcode.

    Do you have any example of uploading a file using a HTML file input and sending that file/URL to pods_attachment_import function?

    I understand that the pods_attachment_import function needs a URL but I think there is not a URL available when uploading files trough PHP as PHP uploads them into a tmp directory and only during the execution of the script. Also the file saved into the tmp directory is placed without extension.

    When you send some file trough PHP, after upload, you move the file created on the tmp directory and save it on another directory indicating name and extension.

    I think this will also help others trying to upload files and relate them into the pods File / Image / Video field type.

    Thank you again for your help.

    Thread Starter Daniel P.

    (@danidub)

    Maybe I’m understanding wrong and first I need to upload the file trough PHP and after the file is uploaded (and I have a full URL) I need to use that URL to pass it on to pods_attachment_import. Tell me what do you think.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘pods_attachment_import uploading wrong and without extension’ is closed to new replies.