11 Replies - 557 Views - Last Post: 04 November 2020 - 11:44 AM

#1 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 413
  • Joined: 13-March 18

fail to upload

Posted 28 October 2020 - 12:28 PM

im trying to upload a picture to a site but i get a timed out error.

the image is not a big image.

the image is this https://ibb.co/Yyv8CVF

when i try to put in an image i made myself it works fine. but this isnt for my use so i have to figure out whats happening with the image failing to upload.

here is the config and code for it.

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
	worker_connections 768;
	# multi_accept on;
}

http {
	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
client_max_body_size 100M;
fastcgi_read_timeout 3000;
fastcgi_buffers 8 128k;
fastcgi_buffer_size 128k;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	gzip on;
	gzip_disable "msie6";

	gzip_vary on;
	gzip_proxied any;
	gzip_comp_level 6;
	gzip_buffers 16 8k;
	gzip_http_version 1.1;
	gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
	
	brotli on;
    brotli_comp_level 6;
    brotli_static on;
    brotli_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/vnd.microsoft.icon image/bmp image/svg+xml;

	ssl_protocols TLSv1.2;
	ssl_ciphers	TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES256-GCM-SHA384;
	
	add_header Strict-Transport-Security "max-age=63072000;";

	ssl_dhparam /home/certs/dhparams.pem;
}



server {
    listen 						80;
    listen 						[::]:80;
    server_name 				www.smokeology.ca smokeology.ca;
	return 						301 https://$server_name;
}
server{
	listen 						443 ssl http2;
	listen						[::]:443 ssl http2;
	server_name 				www.smokeology.ca smokeology.ca;
	ssl_certificate 			/etc/letsencrypt/live/smokeology.ca/fullchain.pem;
	ssl_certificate_key 		/etc/letsencrypt/live/smokeology.ca/privkey.pem;
	root 						/home/smokeology/;
	index 						splash.php;
	
	access_log 					access.log;
	error_log 					error.log;
	
	error_page 401				/401.php; #unauthorized
	error_page 403				/403.php; #forbidden
	error_page 404				/404.php; #not found
	error_page 410				/410.php; #gone
	error_page 500				/500.php; #internal server error
	error_page 500				/502.php; #bad gateway
	
	location ~*  \.(css|woff2)$ {
        expires 365d;
    }
	location ~* \.(eot|ttf|woff|woff2)$ {
		#add_header Access-Control-Allow-Origin *;
	}
	location /lock/ {
        deny all;
    }
	location / {
        try_files $uri $uri/ =404;
    }
	location ~ \.php$ {
        include fastcgi_params;                
		fastcgi_intercept_errors on;
		fastcgi_pass unix:/run/php/php7.4-fpm.sock;
		fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    }
}
server {
    listen 						80;
    listen 						[::]:80;
    server_name 				cdn.smokeology.ca;
	return 						301 https://$server_name;
}
server{
	listen 						443 ssl http2;
	listen						[::]:443 ssl http2;
	server_name 				cdn.smokeology.ca;
	ssl_certificate 			/etc/letsencrypt/live/cdn.smokeology.ca/fullchain.pem;
	ssl_certificate_key 		/etc/letsencrypt/live/cdn.smokeology.ca/privkey.pem;
	root 						/home/smokeology/cdn/;
	index 						index.php;
	
	access_log 					access.log;
	error_log 					error.log;
	
	location ~*  \.(jpg|jpeg|gif|png|svg|ico|pdf|css|js|swf|woff|woff2|ttf)$ {
        expires 365d;
    }
    location / {
        try_files $uri $uri/ =404;
    }
	location ~ \.php$ {
		include fastcgi_params;                
		fastcgi_intercept_errors on;
		fastcgi_pass unix:/run/php/php7.4-fpm.sock;
		fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
	}
	brotli_comp_level			11;
}



if(isset($_GET['id'])){
						$editid = valid($_GET['id']);
						$sql = mysqli_query($conn,"SELECT * FROM inventory WHERE id='$editid'");
						if(mysqli_num_rows($sql) > 0){
							$res = mysqli_fetch_array($sql);
							$link = '&id='.$editid;
							$type = $res['type'];
							$image = $res['image'];
							$name = $res['name'];
							$price = $res['price'];
							$sale = $res['sale'];
							$saletype = $res['saletype'];
							$stock = $res['stock'];
							$description = $res['description'];
						}else{
							Header("Location: ?act=inventory");
						}
					}else{
						$image = 'default.png';
						$type = 'Bong';
						$link = $editid = $name = $price = $saletype = $sale = $stock = $description = '';
					}
					if(isset($_GET['ans'])){
						if($_FILES['fileToUpload']['name'] != "") {
							$target_dir = "cdn/images/products/";
							$target_file = $target_dir.basename($_FILES["fileToUpload"]["name"]);
							$uploadOk = 1;
							$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
							if(isset($_POST["submit"])){
								$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
								if($check !== false){
									echo "File is an image - " . $check["mime"] . ".";
									$uploadOk = 1;
								}else{
									echo "File is not an image.";
									$uploadOk = 0;
								}
							}
							list($width, $height) = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
							/*
							if($width != '400' && $height != '400'){
								echo 'not 400 by 400<br/>';
								echo $width.' - '.$height.'<br/>';
							}
							*/
							if($width != $height){
								echo 'To maintain scalability width and height must be the same.';
								$uploadOk = 0;
							}
							if ($_FILES["fileToUpload"]["size"] > 1000000) {
								echo "Sorry, your file is too large. Max 1 MB";
								$uploadOk = 0;
							}
							if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
								echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
								$uploadOk = 0;
							}
							if($uploadOk == 0){
								echo "Sorry, your file was not uploaded.";
							}else{
								if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "cdn/images/products/".time().'.'.$imageFileType)){
									mysqli_query($conn,"UPDATE inventory SET image='".time().".".$imageFileType."' WHERE id='$editid'")or die(mysqli_error($conn));
									$image = time().'.'.$imageFileType;
									echo 'image uploaded and updated.';
								}else{
									echo "Sorry, there was an error uploading your file.";
								}
							}
						}
						$name = valid($_POST['name']);
						$price = valid($_POST['price']);
						$saletype = valid($_POST['saletype']);
						$sale = valid($_POST['sale']);
						$stock = valid($_POST['stock']);
						$description = valid($_POST['description']);
						if(!empty($editid)){
							mysqli_query($conn,"UPDATE inventory SET name='$name', image='$image', type='$type', price='$price', stock='$stock', saletype='$saletype', sale='$sale', description='$description' WHERE id='$editid'")or die(mysqli_error($conn));
							mysqli_query($conn,"INSERT INTO site_log (`id`,`type`,`date`,`message`) VALUES (null,'Low','".time()."','<a href=\'profile.php?id=".$account['id']."\'>".$account['username']."</a> changed item $name.')")or die(mysqli_error($conn));
							//Header("Location: inventory.php?prodid=$editid");
						}else{
							mysqli_query($conn,"INSERT INTO inventory VALUES (null,'$type','$name','$price','0','$stock','$saletype','$sale','$image','$description','".time()."')")or die(mysqli_error($conn));
							$editid = mysqli_insert_id($conn);
							mysqli_query($conn,"INSERT INTO site_log (`id`,`type`,`date`,`message`) VALUES (null,'Low','".time()."','<a href=\'profile.php?id=".$account['id']."\'>".$account['username']."</a> added item $name.')")or die(mysqli_error($conn));
							//Header("Location: inventory.php?prodid=$editid");
						}
					}



what is going on with this image that it cant get uploaded?

Is This A Good Question/Topic? 0
  • +

Replies To: fail to upload

#2 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2875
  • View blog
  • Posts: 8,276
  • Joined: 15-January 14

Re: fail to upload

Posted 28 October 2020 - 02:26 PM

Well, there are a lot of places to check. Start with the server logs to see if you can find that request and see what the response is. Maybe there's just an issue with bandwidth and the request takes too long and the server times out the request before it finishes. Make sure that PHP is configured to use error logging and check the logs for that also. It doesn't look like you're checking the error code for the actual upload in PHP, so you might also want to check that and see if PHP itself is reporting a problem with the upload that you're not checking for.
Was This Post Helpful? 0
  • +
  • -

#3 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 413
  • Joined: 13-March 18

Re: fail to upload

Posted 28 October 2020 - 02:30 PM

forgot to mention nginx error logs show nothing. any other logs i could check?
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2875
  • View blog
  • Posts: 8,276
  • Joined: 15-January 14

Re: fail to upload

Posted 28 October 2020 - 03:23 PM

Check PHP error logs, depending on how you have PHP set up. Are you using your browser tools to test this? How does the server respond, or does it? How long before the request ends?
Was This Post Helpful? 0
  • +
  • -

#5 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 413
  • Joined: 13-March 18

Re: fail to upload

Posted 28 October 2020 - 06:37 PM

it tries to load for a few minutes then get the 504 error.

i also have this at the start of my scripts.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);


didnt work as i see nothing.

then i went to php.ini and error_log was set to on there is no logs in var/log/nginx or other directories.

i then set error_logs to a directory chown it to www-data:www-data ran the script again and still nothing. no logs.
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6895
  • View blog
  • Posts: 31,745
  • Joined: 10-May 07

Re: fail to upload

Posted 28 October 2020 - 07:52 PM

What is the size of the file that you are trying to upload?
Are you using a CDN like Cloudflare?
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2875
  • View blog
  • Posts: 8,276
  • Joined: 15-January 14

Re: fail to upload

Posted 28 October 2020 - 08:17 PM

The 504 would have been worth mentioning. A 504 means Gateway Timeout. That means that something that is acting as a gateway between your browser and the server is timing out. Saying "a few minutes" isn't all that helpful, but if you verify exactly how long that takes, again using your browser's developer tools, then you can use that value to look for any settings on your server or your CDN or anything else that might be acting as a gateway to look for a timeout matching that length of time.

For example, in your server config, I don't see anything set to "a few minutes," but I do see some timeouts set to 3 minutes. So, does it timeout after 3 minutes, or not? That's the kind of stuff you need to figure out, so instead of "a few minutes," figure out the exact time and see if any of your settings would cause that. If you suspect any of the settings, then change them and see what happens.

Also, in PHP, error_log should be set to a file, not a directory.
Was This Post Helpful? 0
  • +
  • -

#8 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 413
  • Joined: 13-March 18

Re: fail to upload

Posted 29 October 2020 - 04:01 AM

the image is 16KB jpg i wasnt concentrating on the time out period because as i said i can make a simple black image and upload it just fine as a png. no issues whatsoever.

where would the php log be stored then and what value?

my cdn is just a nginx virtual host with brotli compression set to 11 you can look at it from above the code.
Was This Post Helpful? 0
  • +
  • -

#9 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6895
  • View blog
  • Posts: 31,745
  • Joined: 10-May 07

Re: fail to upload

Posted 29 October 2020 - 05:58 AM

Are you checking the logs on the nginx cdn? You may not see any entries in your server logs, because the error isn't happening there.

For troubleshooting, you could turn off your cdn, & send traffic directly to your webserver. Does the problem remain?
Was This Post Helpful? 0
  • +
  • -

#10 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 413
  • Joined: 13-March 18

Re: fail to upload

Posted 29 October 2020 - 11:53 AM

if you look at the conf you will see the domain cdn.domain.ca simply points to a folder on the same machine. its no different.

so it was always sending traffic to my server.

i checked all nginx logs and the logs defined in the domain.conf and there is nothing.

server_name                 cdn.smokeology.ca;
ssl_certificate             /etc/letsencrypt/live/cdn.smokeology.ca/fullchain.pem;
ssl_certificate_key         /etc/letsencrypt/live/cdn.smokeology.ca/privkey.pem;
root                        /home/smokeology/cdn/;
index                       index.php;


Was This Post Helpful? 0
  • +
  • -

#11 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 413
  • Joined: 13-March 18

Re: fail to upload

Posted 03 November 2020 - 10:20 PM

im pulling my hair right now.

it works for 1 image but it doesnt for another? and it doesnt take long at all.

I put in a basic script and it still times out on a specific image...

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
?>



and the 2 images are....


https://smokeology.ca/1.png
https://smokoelogy.ca/2.jpeg

im throwing up phpinfo too...

https://smokeology.ca/phpinfo.php

and the test script...

https://smokoelogy.ca/test.html

This post has been edited by Bobby_Bubbles: 04 November 2020 - 07:05 AM

Was This Post Helpful? 0
  • +
  • -

#12 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2875
  • View blog
  • Posts: 8,276
  • Joined: 15-January 14

Re: fail to upload

Posted 04 November 2020 - 11:44 AM

If you replace all of that PHP code with this:

<?php

print_r($_FILES);


I think you'll find that PHP isn't an issue here, and that there's a problem with your configured gateway. That's where all of your attention should be, not on PHP code. I don't think you can even write PHP code to cause a 504, because the server isn't going to execute PHP at all until the upload completes, and it doesn't look like that's even happening. If you're using php-fpm, I see some people reporting issues with that causing 504 in nginx. But I would focus on that, not on your PHP code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1