Php Browse a file then get its path

15,179

Use pathinfo() http://php.net/manual/en/function.pathinfo.php

Share:
15,179
electricfeel1979
Author by

electricfeel1979

Noob at Programming

Updated on June 04, 2022

Comments

  • electricfeel1979
    electricfeel1979 almost 2 years

    Been stumbling around the net having a difficulty finding a sample of getting a file path of a browsed file. Some of them I read that this function no longer works anymore due to security reasons. Any ideas guys? I badly need a sample. I already tried many times.

    This is currently I have:

    <html>
    
    <body>
    
    
    <form method="post" action="file.php" enctype="multipart/form-data">
    
    <input type="radio" name="type" value="moodle" required="required" /> Moodle<br />
    <input type="radio" name="type" value="bb" required="required" /> BlackBoard<br />
    <input type="file" name="file" id="file" /><br /><br />
    
    <input type="submit" value="Submit" name="submit" />
    
    </form> 
    
    </body>
    
    </html>
    

    the file.php

    <?php
    
    
    $filetype = $_POST["type"];
    
    
    
    
    echo $filetype;
    
    echo "Upload: " . $_FILES['file']['name'] . "<br />";
    echo "Type: " . $_FILES['file']['tmp_name'];
    
    if ($_FILES['file']['error'] > 0)
      {
      echo "Error: " . $_FILES["file"]["error"] . "<br />";
      }
    else
      {
      echo "Upload: " . $_FILES['file']['name'] . "<br />";
      echo "Type: " . $_FILES['file']['type'] . "<br />";
      echo "Size: " . ($_FILES['file']['size'] / 1024) . " Kb<br />";
      echo "Stored in: " . $_FILES['file']['tmp_name'];
      }
    
    
    function moodlezip($zipfile){
    
        echo "<h1>MOODLE</h1>"."<br />";
    
        $moodle = new Moodle();
        $zip = zip_open($zipfile);
        $ziparc = new ZipArchive;
    
    
        if ($zip)
        {
            while ($zip_entry = zip_read($zip))
            {
    
                $file = zip_entry_name($zip_entry);
    
                //echo "Name: " . $file . "<br />";
    
    
                if (strpos($file,'course.xml') !== false) {
    
                    if ($ziparc->open($zipfile) === TRUE) {
    
                        $coursexml =  new SimpleXMLElement($ziparc->getFromName($file));
    
                        $moodle->getCourse($coursexml);
    
                        $ziparc->close();
    
    
                    } else {
                        echo 'failed';
                    }
    
    
                }
    
                else if (strpos($file,'forum.xml') !== false) {
    
                    if ($ziparc->open($zipfile) === TRUE) {
    
                        $topicxml =  new SimpleXMLElement($ziparc->getFromName($file));
    
                        $moodle->getTopic($topicxml);
    
                        $ziparc->close();
    
    
                    } else {
                        echo 'failed';
                    }
    
    
                }
    
    
    
                else if (strpos($file,'lesson.xml') !== false) {
    
                    if ($ziparc->open($zipfile) === TRUE) {
    
                        $lessonxml =  new SimpleXMLElement($ziparc->getFromName($file));
    
                        $moodle->getLessons($lessonxml);
    
                        $ziparc->close();
    
    
                    } else {
                        echo 'failed';
                    }
    
    
                }
    
    
    
                else if (strpos($file,'quiz.xml') !== false) {
    
                    if ($ziparc->open($zipfile) === TRUE) {
    
                        $quizxml =  new SimpleXMLElement($ziparc->getFromName($file));
    
                        $moodle->getQuizzes($quizxml);
    
                        $ziparc->close();
    
    
                    } else {
                        echo 'failed';
                    }
    
    
                }
    
    
                else if (strpos($file,'questions.xml') !== false) {
    
                    if ($ziparc->open($zipfile) === TRUE) {
    
                        $questionsxml =  new SimpleXMLElement($ziparc->getFromName($file));
    
                        $moodle->getQuestions($questionsxml);
    
                        $ziparc->close();
    
    
                    } else {
                        echo 'failed';
                    }
    
    
                }
    
    
            }
    
            zip_close($zip);
        }
    
    }
    
    require_once 'classes/backup.php';
    
    $connect = mysql_connect("localhost","root","");
    
    
    if (!$connect)
    {
        die('Could not connect: ' . mysql_error());
    }
    
    mysql_close($connect);
    
    
    //form upload loop folders input submit, find, bb, scorm
    //class admin
    ?>
    
  • Quentin
    Quentin over 11 years
    That gets the place the file was uploaded to, not the place that it was uploaded from (which is blocked (as the question admits) for security (and privacy) reasons).
  • electricfeel1979
    electricfeel1979 over 11 years
    okay I will to apply it in a form.
  • electricfeel1979
    electricfeel1979 over 11 years
    so what ways now can I do this? if the said above is useless
  • gen_Eric
    gen_Eric over 11 years
    @mrpotatohead1251: You need to first tell us what exactly you are trying to do.
  • user1477388
    user1477388 over 11 years
    "When a user sends binary data to your server via the <file> HTML element, you do not have access to the path under which the data resided - you only have the data. " Ref. bytes.com/topic/php/answers/…. I can only help you get the data that is available.
  • electricfeel1979
    electricfeel1979 over 11 years
    i dont need the file name haays
  • electricfeel1979
    electricfeel1979 over 11 years
    okay like this. the user will upload the backup file of moodle which is in a zip type. then its path to be pass on a function which needs the file path of the zip file
  • Chris
    Chris over 11 years
    @mrpotatohead1251 Yes, well you can't get anything other than the filename -- the file path is simply made unavailable by the browser. Sorry.
  • electricfeel1979
    electricfeel1979 over 11 years
    okay I guess. So what options do I have?. The class I made with its function really need the file to be specific zip file. Can I just upload it somewhere?
  • user1477388
    user1477388 over 11 years
    @mrpotatohead1251: Sorry for not understanding you. Are you trying to get the path to a file on the server or on the user's local machine? If it's the server, see my answer above. If it's the local machine, it is my understanding that this is not possible.
  • electricfeel1979
    electricfeel1979 over 11 years
    sir its in the local machine. So I will just forget about the file path. And go straight with uploading the zip and store it somewhere else. I will try that.