• I am wokring in a sound project adding mp3 files from frontend, the mp3 files get uploaded via zip format and after that WordPress handle this by unziping the mp3 files, after this need to be added into WordPress media, but without success. The files added are not fully correct and without name, any idea or help about this?

    http://laravel.io/bin/nQl3k

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m sort of guessing here, I haven’t fully researched this, but it is a rather educated guess. Usually one would use media_handle_upload() to take an upload request and process it into an attachment. However, the zip format precludes this. (unless there is an action that could be hooked in the middle of the process to do the extraction?)

    I believe you want to use media_handle_sideload(). It works very much like the upload function, but the file already exists somewhere, it does not need to be uploaded, but all the other attachment stuff needs to happen still. A sideload does this for you.

    Thread Starter klevi_surf

    (@klevi_surf)

    It works with absolute path from what i searched, but if i upload in front end will not be possible. i upload the zip file, extract it, and when i pass the data to media_handle_sideload will not work.

    Moderator bcworkz

    (@bcworkz)

    Again, I’m guessing. If you are extracting by script, it may be the script reaches media_handle_sideload before the extraction is complete. I don’t know anything about extracting files via script, but shouldn’t there be something to check indicating if the extraction is done or not?

    Also try the function using a known existing file to be sure there is not an error in your code. Also be sure the folder permissions are correct, the function likely fails silently when permissions issues are encountered.

    Thread Starter klevi_surf

    (@klevi_surf)

    First of all thank you for the support from your side :).
    I tested the media_handle_sideload and if the link is already on the server and accessible from outside it is ok and work with sideload, but what i am trying to do (what i see like a solution) is to upload via zip, extract and after this to scan the directory with php scandir and for every file founded to use sideload, but the only problem i am facing is the fact that the link is not accessible with external link, and i think this is where i stuck. any idea about this?

    Moderator bcworkz

    (@bcworkz)

    Thanks for doing that test. It confirms both that your code is correct and my suspicion that the code is executing before the unzip process has completed. You need some way of ensuring the process is complete before proceeding. Unfortunately, I’m not familiar with unzipping via script. There must be something to check to confirm completion.

    I believe the unzip process works on a temp file which is renamed once the extraction is finished. You could maybe run a while loop checking for the extracted file? Once it appears, it should be safe to exit the loop and go on to the sideload function.

    Note that extracting large files can take longer than the default 30 sec script time, causing the script to time out before the file is fully extracted. If you expect to deal with large files you will need to extend the allowed script time.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add attachment into WordPress Programatically’ is closed to new replies.