Question

I am trying to not allow the uploading of files that have nudity to my server. I found javascript online that will scan a photo for nudity. It comes with demo pics and an html file and js files. I am using PHP to upload the file and I am having trouble not allowing if the scan find that the pic has nudity.

Here is my code sample:

$q= "insert into $table values('', '$email', '$aim', '$icq', '$yahoo', '$homepage', '0', '0', '0', '0', '0', '0', '', now(),'$myip','$email2','$password','$title','$download','$approved','$allowdelete','$author','$facebook','$piclink','$domain','$option3','$secret')";
    $result = mysql_query($q) or die("Failed: $sql - ".mysql_error());
    $q = "select max(id) from $table";
    $result = mysql_query($q);
    $resrow = mysql_fetch_row($result);
    $id = $resrow[0];
    $file = $_FILES['file']['name'];
    move_uploaded_file($_FILES['file']['tmp_name'], "pics/".$id.".".$picext);
    $picfile=$id.".".$picext;
    echo '<script type="text/javascript" <src="nude.js">';
    echo 'nude.load("pics/".<? echo $picfile; ?>);nude.scan(function(result){if(!result){ <? $nude = false; ?>;}else{ $nude = true;}})';
    echo '</script>';
if ($nude === false) { 
    $q = "update $table set picfile = '".$id.".".$picext."' where id='$id'";
    $result = mysql_query($q);
    Header("Location: index.php?id=$id");
    } else{
    echo '<script type="text/javascript">';
    echo 'alert("Nudity found. Please try again.")';
    echo '</script>';
    $q = "delete from $table where id='$id'";
    $result = mysql_query($q);
    unlink("pics/".$picfile);
    Header("Location: new2.php");
    }

The code uploads the file and then it's supposed to check the file for nudity and delete it and tell the user to try again if nudity is found. If nudity is not found the user is brought to the main page of the site.(This is the add new photo page). All of the PHP is working fine, but since the javascript doesn't seem to be running the file i uploaded and then since $nude isn't set it goes into the else of the if statement and again the js doesnt run(no alert box), and then the file is deleted. How can I make the javascript run to scan my uploaded pic for nudity? What am I doing wrong here?

Any help is greatly appreciated!

P.S.

For those that would like to see the js file that is doing the scanning: http://pastebin.com/MpG7HntQ

Was it helpful?

Solution

The problem is that this line:

echo 'nude.load("pics/".<? echo $picfile; ?>);nude.scan(function(result){if(!result){ <? $nude = false; ?>;}else{ $nude = true;}})';

Doesn't do what you think it does.

When you output JavaScript via echo(), that code runs on the browser or client side and doesn't run until after the PHP script has finished.

You'll either need to port the code to PHP or use an AJAX call to report the validity of the images.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top