1

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

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Alex Bridges
  • 67
  • 1
  • 2
  • 10
  • 1
    `echo ' – DutGRIFF Jan 16 '14 at 00:38
  • 1
    If this fixes your problem I will put it in an answer. It is hard to tell what else is wrong other than some ugliness that could be cleaned up a bit. I will add a few cleanups to the answer if this fixed your problem. – DutGRIFF Jan 16 '14 at 00:45
  • I know there is a lot of ugliness. I write code a lot neater than this, but this is something I am doing for my dad and his website. I am just trying to add this feature to his already coded website. That did not fix my problem, but thank you! – Alex Bridges Jan 16 '14 at 01:13

1 Answers1

1

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.

  • Any idea on which is easier/how to do one of them? – Alex Bridges Jan 16 '14 at 01:31
  • It depends-- generally speaking, I don't like to _depend_ on JS for functionality so if it were my app I'd want to have a PHP-side script capable of doing whatever checks your script does. However, the _easiest_ of the two would be to check the uploaded image pre-upload. You should be able to alter the code [here](http://stackoverflow.com/questions/13572129/is-it-possible-to-check-dimensions-of-image-before-uploading) – WildcardSearch Jan 16 '14 at 15:03
  • I actually found a php version and got it working. The only problem I have now is I don't know how to alert the user why there photo isn't being uploaded. It just reloads the page. Any ideas How to communicate to the client that the php found nudity in the pic? I just need some kind of messagebox or something. http://pastebin.com/HsdBCB3S - This is the PHP http://pastebin.com/7jy6baXV - This is the HTML – Alex Bridges Jan 16 '14 at 15:17
  • Flashing a message back to the user can happen in several different ways. You can pass an error code through `$_GET` to the redirected page: `header("Location: index.php?error=1');` and then interpret it-- or you can store flash messages in a database table and recall them on each page load. – WildcardSearch Jan 16 '14 at 15:32
  • I already showed you: `header("Location: index.php?error=1');` . . . then on page load check `if (isset($_GET['error'])) { // do something }` – WildcardSearch Jan 16 '14 at 16:15
  • What's wrong with this code? It is still just reloading the page. http://pastebin.com/4C6V83cG – Alex Bridges Jan 16 '14 at 16:52
  • You are immediately (or after a sleep of 5) redirecting the user-- instead, show the error message at the top of the page (only if there is an error ofc) and then continue with the normal page display. Also, can you accept my answer :-/ – WildcardSearch Jan 16 '14 at 19:34