1

i´d like to save videos on my server via php script. i pass a filename and insert it to my $file variable which is the actual file path to save. echo is correct (Videos/2.mp4), but the saved file has no filename (Videos/.mp4).

<?php


 $filename = $_POST['filename'];

// File Path to save file
$file = 'Videos/'.$filename.'.mp4';

echo $file;

// Get the Request body
$request_body = @file_get_contents('php://input');

// Get some information on the file
$file_info = new finfo(FILEINFO_MIME);

// Extract the mime type
$mime_type = $file_info->buffer($request_body);

// Logic to deal with the type returned
switch($mime_type) 
{
    case "video/mp4; charset=binary":

        // Write the request body to file
        file_put_contents($file, $request_body);

        break;

    default:
        // Handle wrong file type here
}

can anyone help?

2
  • How do you send your form? Commented Oct 22, 2017 at 21:44
  • from an iOS App via NSURLSession: NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:URL];NSString * params = [NSString stringWithFormat:@"filename=%lu", uploadID]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; Commented Oct 22, 2017 at 21:47

1 Answer 1

1

In order to save video's or other files (photo's, documents etc), you have to add enctype="multipart/form-data" to your form.

Like so:

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="file" /> 
    <input type="submit" name="submit" value="Submit" />
</form>

Note: This question tells you how to achieve this in Obj-c. POST multipart/form-data with Objective-C

After that, you are able to read the data through the $_FILES super global. You can use the following script as an example.

<?php

if (!isset($_FILES['file'])) {
    // Handle file not submitted
} else {
    $allowedExtensions = array('video/mp4');
    $mimeType = $_FILES['file']['type'];

    $fileName = $_FILES['file']['name'];
    $fileLocation = 'Videos/' . $fileName;

    if (!in_array($mimeType, $allowedExtensions)) {
        // Handle wrong file type here
    } else {
        move_uploaded_file($_FILES['file']['tmp_name'], $fileLocation);
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

thank you for your quick answer. is this really necessary? I'm able to upload the video, but with no filename. if i open my Videos directory there is the file i've uploaded. but with no filename like this: ".mp4". i´ve checked the echo and and it was: "2.mp4" just how i passed it to the $filename variable.
It's the most recommended way. If it's working and the filename is not that important, you could also hash the filename as like : uniqid(time() . '_')
problem is i need unique identifiers and these would be the filenames. i just edited the file path from $file = 'Videos/'.$filename.'.mp4';to $file = 'Videos/'test.mp4'; and it worked. i see the "test.mp4" in my videos directory..
If you're generating the filename in the client (app), then you're putting yourself in risk with a vulnerability. What would happen if I use the filename "../index.php"? It would override the index.php.
ok but for my private purposes i do want the most code in xcode and objective-c. I'm new to php
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.