Results 1 to 6 of 6

Thread: PHP to check/test text-content on another site.

  1. #1
    Join Date
    Oct 2008
    Location
    Sweden
    Beans
    85
    Distro
    Ubuntu 13.04 Raring Ringtail

    PHP to check/test text-content on another site.

    I'm new to PHP, I'm creating a page where people can upload zip/rar-files, and I'd be happy to receive some help. And I hope it will be a fun challange to someone who knows the ins and out's of php. For this, I have a form with 3 text fields, 1 button for file browsing, and one send/submit button.

    What I want is, for these 3 text fields to hold the send/submit-button hostage (preferably grayed out until the fields are filled out).

    One of the fields is an input for youtube-account name, so, I'd like the page to check "http://www.youtube.com/user/$USERINPUT" if the page contains the text "404 Not Found".
    This check should be performed "live".

    So far, the code looks like this.

    Code:
    <?php include("head.php"); ?>
    
    <div id='cssfield1'>
    
    <?php
    if($_FILES["zip_file"]["name"]) {
            $filename = $_FILES["zip_file"]["name"];
            $source = $_FILES["zip_file"]["tmp_name"];
            $type = $_FILES["zip_file"]["type"];
            $map = $_POST["map"];
            $youtube = $_POST["youtube"];
            $email = $_POST["email"];
    
            $name = explode(".", $filename);
            $accepted_types = array('application/x-rar-compressed', 'application/octet-stream', 'application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
            foreach($accepted_types as $mime_type) {
                    if($mime_type == $type) {
                            $okay = true;
                            break;
                    }
            }
    
            $continue = strtolower($name[1]) == 'zip' ? true : false;
            if(!$continue) {
                    $message = "Filen du försöker ladda upp är inte en zip eller rar-fil. Försök igen.";
            }
    
            $target_path = "/home/valpskott.se/CUSTOM/".$filename;
            if($okay == true) {
                    if(move_uploaded_file($source, $target_path)) {
                            $map = preg_replace("/[\s_]/", "ZPACE", $map);
                            $map = preg_replace("/[^a-öA-Ö0-9\s.]/", "", $map);
                            $youtube = preg_replace("/[\s_]/", "ZPACE", $youtube);
                            $youtube = preg_replace("/[^a-öA-Ö0-9\s.]/", "", $youtube);
                            $filename = preg_replace("/[\s_]/", "ZPACE", $filename);
                            $filename = preg_replace("/[^a-öA-Ö0-9\s.]/", "", $filename);
                            exec ("CUSTOM/unpack.sh $map $youtube $filename $email");
                            $message = "Din fil har laddats upp och processats.";
                    } else {
                            $message = "Det uppstod problem med din uppladdning. Försök igen.";
                    }
            }
    }
    ?>
    
    <form enctype="multipart/form-data" method="post" action="">
    <label for="map">Mappens namn:</label><br>
    <input type="text" name="map" id="map"><br>
    
    <label for="youtube">Ditt användarnamn på Youtube:</label><br>
    <input type="text" name="youtube" id="youtube"><br>
    
    <label for="email">E-post:</label><br>
    <input type="text" name="email" id="email"><br>
    
    <label>Fil:</label><br> <input type="file" name="zip_file" />
    <br />
    <input type="submit" name="submit" value="Skicka" />
    </form>
    
    <?php if($message) echo "<p>$message</p>"; ?>
    
    </div>
    
    </center>
    </body>
    </html>
    Any help or pointers is much appreciated.
    Last edited by Valpskott; May 3rd, 2013 at 02:51 PM.

  2. #2
    Join Date
    Jun 2009
    Location
    Land of Paranoia and Guns
    Beans
    194
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: PHP to check/test text-content on another site.

    It would be a lot simpler and easier on your server to do this verification client side. If your visitors are going to be using modern browsers, simply add the "required" attribute to your input tags.
    HTML syntax:
    Code:
    <input type="text" name="email" id="email" required>
    XHTML syntax:
    Code:
    <input type="text" name="email" id="email" required="required" />
    To check the existence of a youtube user, use the developer API. (documented here)
    If you're using jQuery, here's how you would check if a user exists:
    Code:
    //assuming form is a jQuery node of the form in question...
    form.submit(function (event) {
        try {
            $.ajax({
                url: "http://gdata.youtube.com/feeds/api/users/" + $("#youtube").val(),
                dataType: "text",
                method: "GET",
                cache: false,
                statusCode: {
                    404: function () {
                        throw new Error();
                    }
                }
            }).fail(function () {
                throw new Error();
            }).done(function (data) {
                if (data === "User not found") {
                    throw new Error(); //just in case youtube doesn't 404 for some reason
                }
            });
        } catch (e) {
            event.stopPropogation();
            event.preventDefault();
            //user not found, handle it how you like here...
            return false;
        }
        //if control falls through to here, then the user is valid.
        return true;
    });
    One more thing... your html is a mix of XHTML (<emptyelem booleanattr="booleanattr" />) and HTML (<emptyelem booleanattr>) syntax. While this generally isn't a problem, it might be in the future and should be avoided. Both are valid, so pick whichever you like better and use it consistently throughout your code.
    Last edited by epicoder; May 5th, 2013 at 05:15 AM.
    Don't use W3Schools as a resource! (Inconsequential foul language at the jump)
    Open Linux Forums (More foul language, but well worth it for the quality of support and good humor.)
    If you want to discuss W3Schools, please PM me instead of posting.

  3. #3
    Join Date
    Jun 2012
    Location
    Sweden
    Beans
    324
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: PHP to check/test text-content on another site.

    Valpskott, it's fun to see some Swedes here!
    This should work:
    HTML Code:
    <input type="text" name="email" id="email" required>
    Edit: saw that epicoder proposed exactly the same solution that I did.

  4. #4
    Join Date
    Oct 2008
    Location
    Sweden
    Beans
    85
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: PHP to check/test text-content on another site.

    Thanks for your suggestions. The "required" stuff solved one of the problems, thank you both for that.

    I havn't been able to figure out where to insert epicoders jquery script. My friend is comming over this weekend, so, he will help me implement that solution. Again, thanks (tack) both of you!

  5. #5
    Join Date
    Jun 2012
    Location
    Sweden
    Beans
    324
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: PHP to check/test text-content on another site.

    Ingen orsak.
    Glad you found a solution.

  6. #6
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    9,490
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: PHP to check/test text-content on another site.

    If you just want to check the validity of the YouTube URL, you can use the curl functions, or use the fopen() command if you have allow_url_open enabled. fopen("http://www.example.com","r") will return a file pointer resource if the site exists or FALSE if it does not.
    If you ask for help, please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •