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?

NeoGER89
  • 412
  • 4
  • 16
  • How do you send your form? – shukshin.ivan Oct 22 '17 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]];` – NeoGER89 Oct 22 '17 at 21:47

1 Answers1

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);
    }
}
Jordi Kroon
  • 2,607
  • 3
  • 31
  • 55
  • 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. – NeoGER89 Oct 22 '17 at 22:14
  • 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() . '_')` – Jordi Kroon Oct 22 '17 at 22:16
  • 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.. – NeoGER89 Oct 22 '17 at 22:30
  • 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. – Jordi Kroon Oct 22 '17 at 22:34
  • ok but for my private purposes i do want the most code in xcode and objective-c. I'm new to php – NeoGER89 Oct 22 '17 at 22:45
  • Client is not private. Server is private. Would you trust users, I'd rather not. – Jordi Kroon Oct 22 '17 at 22:47
  • I agree with you but in this case, the app is only for me – NeoGER89 Oct 22 '17 at 22:55